Awesome
stream-from-promise
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