Simplify handling of promises as values.
This commit is contained in:
parent
cdfb4655b5
commit
58fe68f1c4
2 changed files with 8 additions and 16 deletions
|
@ -60,14 +60,9 @@ const Channel = function(length = 0) {
|
||||||
index.push++;
|
index.push++;
|
||||||
} else if (shift.cancelled) {
|
} else if (shift.cancelled) {
|
||||||
index.shift++;
|
index.shift++;
|
||||||
} else {
|
|
||||||
if (`reason` in push) {
|
|
||||||
shift.reject(push.reason);
|
|
||||||
} else {
|
} else {
|
||||||
lastValue = push.value;
|
lastValue = push.value;
|
||||||
shift.resolve(lastValue);
|
shift.resolve(lastValue);
|
||||||
}
|
|
||||||
|
|
||||||
buffered = Math.max(0, buffered - 1);
|
buffered = Math.max(0, buffered - 1);
|
||||||
index.push++;
|
index.push++;
|
||||||
index.shift++;
|
index.shift++;
|
||||||
|
@ -323,9 +318,7 @@ const Channel = function(length = 0) {
|
||||||
// If value is a promise that rejects, catch it in case there hasn't
|
// 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
|
// been a matching shift yet in order to prevent an unhandledRejection
|
||||||
// error. Reject it again when there's a shift.
|
// error. Reject it again when there's a shift.
|
||||||
Promise.resolve(value).catch(reason => {
|
Promise.resolve(value).catch(() => {});
|
||||||
order.reason = reason;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (closed) {
|
if (closed) {
|
||||||
order.reject(new Error(`Can't push to closed channel.`));
|
order.reject(new Error(`Can't push to closed channel.`));
|
||||||
|
|
|
@ -455,13 +455,12 @@ describe(`Channel object`, function() {
|
||||||
it(`allows promises to be sent through a channel`, function() {
|
it(`allows promises to be sent through a channel`, function() {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
process.once(`unhandledRejection`, reject);
|
process.once(`unhandledRejection`, reject);
|
||||||
|
const channel = Channel();
|
||||||
|
|
||||||
const channel = Channel.of(
|
(async () => {
|
||||||
Promise.resolve(`resolved`),
|
await channel.push(Promise.resolve(`resolved`));
|
||||||
new Promise((resolve, reject) => {
|
await channel.push(Promise.reject(new Error(`rejected`)));
|
||||||
setImmediate(reject, new Error(`rejected`));
|
})();
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
assert.equal(await channel.shift(), `resolved`);
|
assert.equal(await channel.shift(), `resolved`);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue