diff --git a/lib/index.js b/lib/index.js index 66d5e29..f90b03b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -331,39 +331,42 @@ Channel.from = values => { }; Channel.of = (...values) => Channel.from(values); - Channel.isChannel = arg => prototype.isPrototypeOf(arg); Channel.select = methodPromises => Object.assign( new Promise((resolve, reject) => { - methodPromises.forEach(async promise => { - try { - 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 { + methodPromises.forEach(async promise => { try { - await promise; - } catch (exception) { - reject(exception); - `Channel.select accepts only promises returned by push & shift.` - } + 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); - } catch (exception) { - reject( - new TypeError( - ) - ); - } - }); + try { + await promise; + } catch (exception) { + reject(exception); + } + + resolve(promise.channel); + } catch (exception) { + reject( + new TypeError( + `Channel.select accepts only promises returned by push & shift.` + ) + ); + } + }); + } catch (exception) { + reject(new TypeError(`Channel.select: Argument must be an array.`)); + } }), { cancel: () => Promise.all(methodPromises.map(promise => promise.cancel())) diff --git a/test/index.js b/test/index.js index 3009ee2..90d1301 100644 --- a/test/index.js +++ b/test/index.js @@ -124,6 +124,12 @@ describe(`Channel`, function() { await Channel.select([Promise.resolve()]); }, new TypeError(`Channel.select accepts only promises returned by push & shift.`)); }); + + it(`complains if not given an array`, function() { + assertRejects(async () => { + await Channel.select(Channel.of(0).shift(), Channel.of(1).shift()); + }, new TypeError(`Channel.select: Argument must be an array.`)); + }); }); describe(`functional interface`, async function() {