Git Product home page Git Product logo

stream-from-promise's Introduction

stream-from-promise Dependencies Status Image Build Status Image Coverage Status

Create streams from ECMAScript 6 Promises (ES.next Draft Rev 25).

npm install stream-from-promise --save

Usage

As long as es6 is a draft:

npm install promise --save

String | Buffer promises

var StreamFromPromise = require('stream-from-promise'),
    Promise = require('promise');

var stringPromise = new Promise(function(resolve, reject) {
  setTimeout(function() { resolve('strrrring!'); }, 500);
});

StreamFromPromise(stringPromise)
  .pipe(process.stdout); // output: strrrring!


var bufferPromise = new Promise(function(resolve, reject) {
  setTimeout(function() { resolve(new Buffer('buff!')); }, 500);
});

StreamFromPromise(bufferPromise)
  .pipe(process.stdout); // output: buff!

Arbitrary Promises

var StreamFromPromise = require('stream-from-promise'),
    Promise = require('promise');

function logFunc(){
  console.log('func!?!');
};

var funcPromise = new Promise(function(resolve, reject) {
  setTimeout(function() { resolve(logFunc) }, 500);
});

StreamFromPromise.obj(funcPromise)
  .on('data', function(fn){
    fn(); // output: func!?!
  });

Rejecting

var StreamFromPromise = require('stream-from-promise'),
    Promise = require('promise');

var rejectPromise = new Promise(function(resolve, reject) {
  setTimeout(function() { reject(new Error('rejected')) }, 500);
});

StreamFromPromise(rejectPromise)
  .on('error', function(err){
    console.log(err); // output: [Error: rejected]
  })
  .on('data', function(data){
    // do something awsome
  });

Gulp File promises

Gulp files are vinyl files:

npm install vinyl

Test some awsome Gulp plugin:

var StreamFromPromise = require('stream-from-promise'),
    Promise = require('promise'),
    File = require('vinyl');

var hello = new File({
      cwd: '/',
      base: '/hello/',
      path: '/hello/hello.js',
      contents: new Buffer('console.log("Hello");')
    });

var helloFilePromise = new Promise(function(resolve, reject) {
  setTimeout(function() { resolve(hello) }, 500);
});

StreamFromPromise.obj(helloFilePromise)
  .pipe(someAwsomeGulpPlugin())
  .on('data', function(file){
    console.log(file.contents.toString()); // dunno what someAwsomeGulpPlugin does :)
  });

See also stream-recorder for testing gulp plugins.

API

Class: StreamFromPromise

StreamFromPromises are Readable streams.

new StreamFromPromise(promise, [options])

  • promise Promise ECMAScript 6 Promises returning Javascript values like numbers, strings, objects, functions, ...
  • options Object passed through new Readable([options])

Note: The new operator can be omitted.

StreamFromPromise#obj(promise, [options])

A convenience wrapper for new StreamFromPromise(promise, {objectMode: true, ...}).

License

Copyright (c) 2014 Michael Mayer

Licensed under the MIT license.

stream-from-promise's People

Contributors

schnittstabil avatar

Watchers

James Cloos avatar Loïc Mahieu avatar  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.