Skip to content
Closed
Changes from 1 commit
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
Next Next commit
docs: fix stream async iterator sample
The for await loop into writable loop could cause an unhandled exception
in the case where we are waiting for data from the async iterable and
this no `'error'` handler is registered on the writable.

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

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

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

(async function() {
// Ensure completion without errors.
await finished(writable);
await Promise.all([
pump(iterator, writable),
finished(writable)
]);
})();
```

Expand Down