diff options
author | Robert Nagy <ronagy@icloud.com> | 2020-01-15 12:29:06 +0300 |
---|---|---|
committer | Robert Nagy <ronagy@icloud.com> | 2020-01-25 02:06:31 +0300 |
commit | 4fefd181c759f3e8a7297bb3f9203d2c7841b14a (patch) | |
tree | 13e27d5483682741515ceee658514b7f020a2e76 /doc/api/stream.md | |
parent | 8d14a8cbcee313591138d233e9944664ea6186bc (diff) |
doc: 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: https://github.com/nodejs/node/issues/31365
PR-URL: https://github.com/nodejs/node/pull/31367
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Diffstat (limited to 'doc/api/stream.md')
-rw-r--r-- | doc/api/stream.md | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/doc/api/stream.md b/doc/api/stream.md index cf03163218b..82cf99b4332 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -2682,12 +2682,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(); |