GeezerVison is showing github.com — doc: fix stream async iterator sample · nodejs/node@d51de78
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: doc/api/stream.md +11-3Lines changed: 11 additions & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2644,15 +2644,23 @@ const finished = util.promisify(stream.finished); |
2644 | 2644 |
|
2645 | 2645 | const writable = fs.createWriteStream('./file'); |
2646 | 2646 |
|
2647 | | -(async function() { |
| 2647 | +async function pump(iterator, writable) { |
2648 | 2648 | for await (const chunk of iterator) { |
2649 | 2649 | // Handle backpressure on write(). |
2650 | | - if (!writable.write(chunk)) |
| 2650 | + if (!writable.write(chunk)) { |
| 2651 | + if (writable.destroyed) return; |
2651 | 2652 | await once(writable, 'drain'); |
| 2653 | + } |
2652 | 2654 | } |
2653 | 2655 | writable.end(); |
| 2656 | +} |
| 2657 | + |
| 2658 | +(async function() { |
2654 | 2659 | // Ensure completion without errors. |
2655 | | - await finished(writable); |
| 2660 | + await Promise.all([ |
| 2661 | + pump(iterator, writable), |
| 2662 | + finished(writable) |
| 2663 | + ]); |
2656 | 2664 | })(); |
2657 | 2665 | ``` |
2658 | 2666 |
|
|