Awesome
callback2stream
Transform sync, async or generator function to Stream. Correctly handle errors and optional arguments.
Install
npm i callback2stream --save
Usage
For more use-cases see the tests
const callback2stream = require('callback2stream')
callback2stream
Create a stream from sync, async or generator function.
Params
fn
{Function}: Any kind of function.opts
{Object}: Directly passed to through2 from promise2stream.returns
{Function}: That when executed, returns transform stream, coming from promise2stream, using through2.
Example
var fs = require('fs')
var cb2stream = require('callback2stream')
var readFileStream = cb2stream(fs.readFile)
var stream = readFileStream('package.json', 'utf8')
stream
.on('data', function (val) {
var json = JSON.parse(val)
console.log(json.name) // => 'callback2stream'
})
.once('error', console.error)
.once('end', function () {
console.log('reading finished')
})
// you also have access to the
// contents with promise
stream.promise
.then(JSON.parse, console.error)
.then(function (val) {
console.log(val.name) // => 'callback2stream'
}, console.error)
.catch(console.error)
Related
- letta: Let's move to promises! Drop-in replacement for
co@4
(passing 100% tests), but… more | homepage - limon: The pluggable JavaScript lexer. Limon = Lemon. | homepage
- postjson: Transforming JSON with plugins. | homepage
- promise2stream: Transform ES2015 Promise to Stream - specifically, Transform Stream. homepage
- promise2thunk: Convert (transform) promise to thunk, just like was in co@3 | homepage
- relike: Simple promisify a callback-style function with sane defaults. Support promisify-ing sync functions. | homepage
- stream-from-promise: Create streams from promises | homepage
- stream-to-promise: Convert streams (readable or writable) to promises | homepage
- through2: A tiny wrapper around Node streams2 Transform to avoid explicit subclassing noise | homepage
- thunk2promise: Transform or convert thunk to Bluebird Promise. | homepage
- value2stream: Transform any value to stream. Create stream from any value - string,… more | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.