Skip to content

Commit

Permalink
stream: make async iterator .next() always resolve
Browse files Browse the repository at this point in the history
See: nodejs/readable-stream#387

PR-URL: #24668
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
mcollina authored and targos committed Nov 29, 2018
1 parent 61e0103 commit b809fa8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/internal/streams/async_iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ function onReadable(iter) {
function wrapForNext(lastPromise, iter) {
return (resolve, reject) => {
lastPromise.then(() => {
if (iter[kEnded]) {
resolve(createIterResult(undefined, true));
return;
}

iter[kHandlePromise](resolve, reject);
}, reject);
};
Expand All @@ -61,7 +66,7 @@ const ReadableStreamAsyncIteratorPrototype = Object.setPrototypeOf({
}

if (this[kEnded]) {
return Promise.resolve(createIterResult(null, true));
return Promise.resolve(createIterResult(undefined, true));
}

if (this[kStream].destroyed) {
Expand All @@ -74,7 +79,7 @@ const ReadableStreamAsyncIteratorPrototype = Object.setPrototypeOf({
if (this[kError]) {
reject(this[kError]);
} else {
resolve(createIterResult(null, true));
resolve(createIterResult(undefined, true));
}
});
});
Expand Down Expand Up @@ -115,7 +120,7 @@ const ReadableStreamAsyncIteratorPrototype = Object.setPrototypeOf({
reject(err);
return;
}
resolve(createIterResult(null, true));
resolve(createIterResult(undefined, true));
});
});
},
Expand All @@ -131,7 +136,6 @@ const createReadableStreamAsyncIterator = (stream) => {
value: stream._readableState.endEmitted,
writable: true
},
[kLastPromise]: { value: null, writable: true },
// the function passed to new Promise
// is cached so we avoid allocating a new
// closure at every run
Expand All @@ -151,6 +155,7 @@ const createReadableStreamAsyncIterator = (stream) => {
writable: true,
},
});
iterator[kLastPromise] = null;

finished(stream, (err) => {
if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') {
Expand All @@ -172,7 +177,7 @@ const createReadableStreamAsyncIterator = (stream) => {
iterator[kLastPromise] = null;
iterator[kLastResolve] = null;
iterator[kLastReject] = null;
resolve(createIterResult(null, true));
resolve(createIterResult(undefined, true));
}
iterator[kEnded] = true;
});
Expand Down
65 changes: 65 additions & 0 deletions test/parallel/test-stream-readable-async-iterators.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,71 @@ async function tests() {
r.destroy(null);
}
})();

await (async () => {
console.log('all next promises must be resolved on end');
const r = new Readable({
objectMode: true,
read() {
}
});

const b = r[Symbol.asyncIterator]();
const c = b.next();
const d = b.next();
r.push(null);
assert.deepStrictEqual(await c, { done: true, value: undefined });
assert.deepStrictEqual(await d, { done: true, value: undefined });
})();

await (async () => {
console.log('all next promises must be resolved on destroy');
const r = new Readable({
objectMode: true,
read() {
}
});

const b = r[Symbol.asyncIterator]();
const c = b.next();
const d = b.next();
r.destroy();
assert.deepStrictEqual(await c, { done: true, value: undefined });
assert.deepStrictEqual(await d, { done: true, value: undefined });
})();

await (async () => {
console.log('all next promises must be resolved on destroy with error');
const r = new Readable({
objectMode: true,
read() {
}
});

const b = r[Symbol.asyncIterator]();
const c = b.next();
const d = b.next();
const err = new Error('kaboom');
r.destroy(err);

await Promise.all([(async () => {
let e;
try {
await c;
} catch (_e) {
e = _e;
}
assert.strictEqual(e, err);
})(), (async () => {
let e;
try {
await d;
} catch (_e) {
e = _e;
}
assert.strictEqual(e, err);
})()]);
})();
}

// to avoid missing some tests if a promise does not resolve
Expand Down

0 comments on commit b809fa8

Please sign in to comment.