Git Product home page Git Product logo

stream-to-promise-agnostic's Introduction

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

stream-to-promise-agnostic's People

Contributors

bendrucker avatar cprecioso avatar marsup avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.