Skip to content

Transform sync, async or generator function to Stream. Correctly handle errors and optional arguments.

License

Notifications You must be signed in to change notification settings

hybridables/callback2stream

Repository files navigation

Transform sync, async or generator function to Stream. Correctly handle errors and optional arguments.

code climate standard code style travis build status coverage status dependency status

Install

npm i callback2stream --save

Usage

For more use-cases see the tests

const callback2stream = require('callback2stream')

Create a stream from sync, async or generator function.

Params

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

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.

tunnckoCore.tk keybase tunnckoCore tunnckoCore npm tunnckoCore twitter tunnckoCore github

About

Transform sync, async or generator function to Stream. Correctly handle errors and optional arguments.

Resources

License

Stars

Watchers

Forks

Packages

No packages published