Socket
Book a DemoInstallSign in
Socket

stream-from-promise

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stream-from-promise

Create streams from promises

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
38K
-10.57%
Maintainers
1
Weekly downloads
 
Created
Source

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

Create streams from ECMAScript 2015 Promises.

npm install stream-from-promise --save

Usage

String | Buffer promises

import StreamFromPromise from 'stream-from-promise';

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

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


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

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

Arbitrary Promises

import StreamFromPromise from 'stream-from-promise';

const funcPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve(() => {
      console.log('func!?!');
    });
  }, 500);
});

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

Rejecting

import StreamFromPromise from 'stream-from-promise';

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

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

Gulp File promises

Gulp files are vinyl files:

npm install vinyl

Test some awsome Gulp plugin:

import StreamFromPromise from 'stream-from-promise';
import File from 'vinyl';

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

const helloFilePromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve(hello);
  }, 500);
});

StreamFromPromise.obj(helloFilePromise)
  .pipe(someAwsomeGulpPlugin())
  .on('data', 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 2015 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

MIT © Michael Mayer

Keywords

readable

FAQs

Package last updated on 24 Feb 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.