1.1.1 • Published 6 years ago

stream-to-promise-agnostic v1.1.1

Weekly downloads
21
License
MIT
Repository
github
Last release
6 years ago

stream-to-promise-agnostic

(Built using TypeScript. Type declarations are embedded.)

Convert streams (readable or writable) to promises using your own standard Promise library.

Usage

Step 1: Import

Using an ES6 style compatible Promise library
import Promise from 'bluebird; // Or any constructor compatible promise like library of your choice.  
import streamToPromise from 'stream-to-promise-agnostic';

const convert = streamToPromise(Promise,true); // 'true' indicates a Promise constructor

streamToPromise can take any PromiseConstructorLike (see lib.d.ts) constructor when the second param equals true

Using a Promise library that doesn't have a constructor (Example: Q)
import {streamToPromise,PromiseFactory,Executor} from 'stream-to-promise-agnostic';
const QPromiseFactory:PromiseFactory = <T>(e:Executor<T>)=>require("q").promise(e);

const convert = streamToPromise(QPromiseFactory);

Step 2: Consume

Readable
convert.toPromise(readableStream).then(array=> {
  // array.length === 3
});
readableStream.emit('data', 1);
readableStream.emit('data', 2);
readableStream.emit('data', 3);
readableStream.emit('end'); // promise is resolved here
Writable
convert.toPromise(writableStream).then(()=> {
  // resolves undefined
});
writableStream.write('data');
writableStream.end(); // promise is resolved here
1.1.1

6 years ago

1.1.0

6 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago