Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: make async iterator .next() always resolve #24668

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: verify that c and d are not done yet.

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);
})()]);
})();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the Promise.all should be obsolete.

}

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