Allow promises to be passed through a channel.

This commit is contained in:
David Braun 2018-03-28 13:35:11 -04:00
parent 965d14d82c
commit 57247c6d10
No known key found for this signature in database
GPG key ID: 87EC41ADF710B7E2
2 changed files with 36 additions and 2 deletions

View file

@ -60,8 +60,13 @@ const Channel = function(length = 0) {
} else if (shift.cancelled) {
index.shift++;
} else {
lastValue = push.value;
shift.resolve(lastValue);
if (`reason` in push) {
shift.reject(push.reason);
} else {
lastValue = push.value;
shift.resolve(lastValue);
}
buffered = Math.max(0, buffered - 1);
index.push++;
index.shift++;
@ -320,6 +325,13 @@ const Channel = function(length = 0) {
const { order, promise } = Order(this);
order.value = value;
// If value is a promise that rejects, catch it in case there hasn't
// been a matching shift yet in order to prevent an unhandledRejection
// error. Reject it again when there's a shift.
Promise.resolve(value).catch(reason => {
order.reason = reason;
});
if (closed) {
order.reject(new Error(`Can't push to closed channel.`));
} else if (value === undefined) {