Skip to content

Latest commit

 

History

History
60 lines (47 loc) · 1.42 KB

README.md

File metadata and controls

60 lines (47 loc) · 1.42 KB

streams-as-promised

Your streams are now promises in your favourite promise library, so you no longer have to choose between using streams and functional programming. An instance of a stream in this sense is either the empty Stream (a promise of an empty Array) or a promise of both a dynamic value and Stream.

    Stream :: EmptyStream | Promise [Dynamic, Stream]

Intro

npm install streams-as-promised

then

var Stream = require('streams-as-promised')(Promise);

where ```Promise```` is your favourite promise library (bluebird is advised).

Examples

To create a stream from an array:

var stream = new Stream([0,1,2,3]);

To read from a node readable stream:

var stream = (new Stream()).read(readable);

To write to a node writable stream:

var sameStreamForChaining = stream.write(writable);

To apply a function fn to every value in a stream:

var mappedStream = stream.map(fn);

using the .then function will return a promise of an array, if you want to chain streams use

    Stream.prototype.next(Function onResolved, Function ifEmpty, [Function onRejected]) -> Stream

here

    onResolved(Dynamic x, Stream xs) -> Stream | [Dynamic, Stream]
    ifEmpty() -> Stream
    onRejected(Dynamic reason) -> Stream

e.g.

var streamOfTypes = stream.next(function(x, xs){
	return [typeof x, xs];
}, function(){
	return [];
});