When writing to a writable stream from an async iterator, ensure correct handling of backpressure and errors. [stream.pipeline()][] abstracts away the handling of backpressure and backpressure-related errors:

    1. const { pipeline } = require('stream');
    2. const util = require('util');
    3. const fs = require('fs');
    4. const writable = fs.createWriteStream('./file');
    5. // Callback Pattern
    6. pipeline(iterator, writable, (err, value) => {
    7. if (err) {
    8. console.error(err);
    9. } else {
    10. console.log(value, 'value returned');
    11. }
    12. });
    13. // Promise Pattern
    14. const pipelinePromise = util.promisify(pipeline);
    15. pipelinePromise(iterator, writable)
    16. .then((value) => {
    17. console.log(value, 'value returned');
    18. })
    19. .catch(console.error);