Use a passthrough stream
import{PassThrough}from'stream';exportmyCoolFunction=()=>{letstream=newPassThrough();asyncFunc((err,otherStream)=>{otherStream.pipe(stream);});}Streamify helps you easily provide a streaming interface for your code.
conststreamify=require('streamify');constrequest=require('request');exports.doSomething=()=>{letstream=streamify();request(url1,(err,res,body)=>{// Do something with `body`.// Once the actual stream you want to return is ready,// call `stream.resolve()`.stream.resolve(request(url2));});// Your function can return back a stream!!returnstream;};// Because `doSomething()` returns a stream, it can be piped.exports.doSomething().pipe(anotherStream);Returns an instance of a stream. options can be
readable- Defaults totrue.writable- Defaults totrue.
Must be called only once when the actual stream you are proxying to becomes available after an asynchronous operation.
Can be used to unbind a a resolved stream to later call resolve() again.
Add a source readable stream.
Remove previously added source stream.
Add a destination writable stream.
Remove a previously added destination stream.
npm install streamify
Tests are written with mocha
npm test