Wait for drain
The same slow writable, but this time the code listens when write() says stop.
const { Writable } = require('node:stream');const slow = new Writable({ objectMode: true, highWaterMark: 2, write(chunk, enc, done) { setTimeout(done, 5); },});const items = ['a', 'b', 'c'];let i = 0;function writeMore() { while (i < items.length) { const ok = slow.write(items[i]); console.log('wrote ' + items[i++]); if (!ok) { console.log('pause'); slow.once('drain', () => { console.log('drain'); writeMore(); }); return; } } console.log('all written');}writeMore();1. Predict the output
Tap the lines in the order you think they print.
- 1…
- 2…
- 3…
- 4…
- 5…
- 6…