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

docs: fix stream async iterator sample #31252

Closed
wants to merge 3 commits into from
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
20 changes: 14 additions & 6 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() {
for await (const chunk of iterator) {
async function pump(iterable, writable) {
for await (const chunk of iterable) {
// Handle backpressure on write().
if (!writable.write(chunk))
if (!writable.write(chunk)) {
if (writable.destroyed) return;
Copy link
Member Author

Choose a reason for hiding this comment

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

This writable.destroyed check is ugly but I don't see a way around it...

await once(writable, 'drain');
}
}
writable.end();
}

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

Expand All @@ -2677,7 +2685,7 @@ const finished = util.promisify(stream.finished);
const writable = fs.createWriteStream('./file');

(async function() {
const readable = Readable.from(iterator);
const readable = Readable.from(iterable);
readable.pipe(writable);
// Ensure completion without errors.
await finished(writable);
Expand All @@ -2692,7 +2700,7 @@ const pipeline = util.promisify(stream.pipeline);
const writable = fs.createWriteStream('./file');

(async function() {
const readable = Readable.from(iterator);
const readable = Readable.from(iterable);
await pipeline(readable, writable);
})();
```
Expand Down