Skip to content
Closed
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
docs: further fix async iterator example
Further fixes an issue with the async iterator example where an
incorrect assumption was made in regards that drain or error
is always invoked after !write().

Fixes: #31365
  • Loading branch information
ronag committed Jan 15, 2020
commit 883d94c56d7457fd002532fdcc8668d9196cefc1
14 changes: 12 additions & 2 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -2647,12 +2647,22 @@ const finished = util.promisify(stream.finished);

const writable = fs.createWriteStream('./file');

function drain(writable) {
if (writable.destroyed) {
return Promise.reject(new Error('premature close'));
}
return Promise.race([
once(writable, 'drain'),
once(writable, 'close')
.then(() => Promise.reject(new Error('premature close')))
]);
}

async function pump(iterable, writable) {
for await (const chunk of iterable) {
// Handle backpressure on write().
if (!writable.write(chunk)) {
if (writable.destroyed) return;
await once(writable, 'drain');
await drain(writable);
}
}
writable.end();
Expand Down