A promise waiting stream ready
npm i await-stream-ready --save
Wait a read stream
const fs = require('fs');
const awaitReadStream = require('await-stream-ready').read;
async function read(file) {
const stream = fs.createReadStream(file);
stream.on('data', buf => {
// consume buf
});
await awaitReadStream(stream);
}
Or wait a write stream
const fs = require('fs');
const awaitWriteStream = require('await-stream-ready').write;
async function write(srcStream, file) {
const stream = srcStream.pipe(fs.createWriteStream(file));
await awaitWriteStream(stream);
}
It will switch the readable stream to flowing mode automatically.