Channel.select: Complain if not passed an array.
This commit is contained in:
parent
32af6e8987
commit
3af150ae49
2 changed files with 35 additions and 26 deletions
55
lib/index.js
55
lib/index.js
|
@ -331,39 +331,42 @@ Channel.from = values => {
|
||||||
};
|
};
|
||||||
|
|
||||||
Channel.of = (...values) => Channel.from(values);
|
Channel.of = (...values) => Channel.from(values);
|
||||||
|
|
||||||
Channel.isChannel = arg => prototype.isPrototypeOf(arg);
|
Channel.isChannel = arg => prototype.isPrototypeOf(arg);
|
||||||
|
|
||||||
Channel.select = methodPromises =>
|
Channel.select = methodPromises =>
|
||||||
Object.assign(
|
Object.assign(
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
methodPromises.forEach(async promise => {
|
try {
|
||||||
try {
|
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.
|
||||||
`Channel.select accepts only promises returned by push & shift.`
|
methodPromises.forEach(other => {
|
||||||
}
|
if (other !== promise) {
|
||||||
|
other.cancel();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
resolve(promise.channel);
|
try {
|
||||||
} catch (exception) {
|
await promise;
|
||||||
reject(
|
} catch (exception) {
|
||||||
new TypeError(
|
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()))
|
cancel: () => Promise.all(methodPromises.map(promise => promise.cancel()))
|
||||||
|
|
|
@ -124,6 +124,12 @@ describe(`Channel`, function() {
|
||||||
await Channel.select([Promise.resolve()]);
|
await Channel.select([Promise.resolve()]);
|
||||||
}, new TypeError(`Channel.select accepts only promises returned by push & shift.`));
|
}, 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() {
|
describe(`functional interface`, async function() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue