Created
September 8, 2022 06:02
-
-
Save psenger/a0a0ac925fbbfcd8facfe5dcf142a9f4 to your computer and use it in GitHub Desktop.
[Piping to Writable Streams from Async Iterators] #JavaScript #NodeJS #Stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { once } = require('events'); | |
const finished = util.promisify(stream.finished); | |
const writable = fs.createWriteStream('./file'); | |
(async function() { | |
for await (const chunk of iterator) { | |
// Handle backpressure on write(). | |
if (!writable.write(chunk)) | |
await once(writable, 'drain'); | |
} | |
writable.end(); | |
// Ensure completion without errors. | |
await finished(writable); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment