Channel.select: Complain when given non-channel method promises.

This commit is contained in:
David Braun 2017-10-18 10:37:07 -04:00
parent b5c96177e4
commit a2746f6e68
2 changed files with 29 additions and 15 deletions

View file

@ -326,23 +326,31 @@ Channel.select = methodPromises =>
Object.assign( Object.assign(
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
methodPromises.forEach(async promise => { 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 { try {
await promise; promise.prethen(() => {
} catch (exception) { // We've been given a heads-up that this method will complete first
reject(exception); // 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.`
)
);
}
}); });
}), }),
{ {

View file

@ -128,6 +128,12 @@ describe(`Channel`, function() {
closed 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() { describe(`functional interface`, async function() {