0.1.0 • Published 11 years ago
streams-as-promised v0.1.0
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-promisedthen
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]) -> Streamhere
onResolved(Dynamic x, Stream xs) -> Stream | [Dynamic, Stream]
ifEmpty() -> Stream
onRejected(Dynamic reason) -> Streame.g.
var streamOfTypes = stream.next(function(x, xs){
return [typeof x, xs];
}, function(){
return [];
});