Convert Node.js Streams to Promise
NPM
npm install then-streams -S
Yarn
yarn add then-streams -S
This package is written in TypeScript, also can work on JavaScript!
Writable Stream
import fs from 'fs';
import thenStreams from 'then-streams';
async function readFileToConsole() {
const stream = fs.createReadStream('some-file.txt')
.pipe(process.stdout);
await thenStreams.toPromise(stream);
console.log('The file has finished reading and pipe to console');
}
Readable Stream
import fs from 'fs';
import thenStreams from 'then-streams';
async function readFileAndCollect() {
const data = [];
const stream = fs.createReadStream('some-file.txt')
.on('data', d => data.push(d));
await thenStreams.toPromise(stream);
}
You could run the tests from the test
folder
yarn install
yarn test
Licensed under the MIT license.