From a2746f6e683ad28f61cc7edcf1d17f7b5d707ee0 Mon Sep 17 00:00:00 2001 From: David Braun Date: Wed, 18 Oct 2017 10:37:07 -0400 Subject: [PATCH] Channel.select: Complain when given non-channel method promises. --- lib/index.js | 38 +++++++++++++++++++++++--------------- test/index.js | 6 ++++++ 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/index.js b/lib/index.js index 5bf5968..aa5664f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -326,23 +326,31 @@ Channel.select = methodPromises => Object.assign( new Promise((resolve, reject) => { methodPromises.forEach(async promise => { - promise.prethen(() => { - // We've been given a heads-up that this method will complete first so - // cancel the other method calls. - methodPromises.forEach(other => { - if (other !== promise) { - other.cancel(); - } - }); - }); - try { - await promise; - } catch (exception) { - reject(exception); - } + promise.prethen(() => { + // We've been given a heads-up that this method will complete first + // so cancel the other method calls. + methodPromises.forEach(other => { + if (other !== promise) { + other.cancel(); + } + }); + }); - resolve(promise.channel); + try { + await promise; + } catch (exception) { + reject(exception); + } + + resolve(promise.channel); + } catch (exception) { + reject( + new TypeError( + `Channel.select accepts only Channel method promises.` + ) + ); + } }); }), { diff --git a/test/index.js b/test/index.js index be85c78..f3eb702 100644 --- a/test/index.js +++ b/test/index.js @@ -128,6 +128,12 @@ describe(`Channel`, function() { closed ); }); + + it(`complains when given non-channel method promises`, function() { + assertRejects(async () => { + await Channel.select([Promise.resolve()]); + }, new TypeError(`Channel.select accepts only Channel method promises.`)); + }); }); describe(`functional interface`, async function() {