Home

Awesome

make-callback npmjs.com The MIT License

Make synchronous function or generator to support callback api

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

Install

npm i make-callback --save
npm test

Usage

For more use-cases see the tests

var makeCallback = require('make-callback')
var JSONParseAsync = makeCallback(JSON.parse)

JSONParseAsync('{"foo":"bar"}', function (err, json) {
  if (err) {
    return console.error(err)
  }

  console.log(json)
  //=> {foo: 'bar'}
})

You also can make generator that return and yield anything (string, array, object, function, thunk, promise or etc) to support callback api

var fs = require('mz/fs')
var makeCallback = require('make-callback')

function * gen (val) {
  var a = yield 'a'
  var b = yield {b: 'b'}
  var c = yield ['c', 'f']
  var d = yield 123
  var e = yield fs.readFile('./package.json')
  return [a, b, c, d, e, val || 'foobar']
}

var genCallback = makeCallback(gen)
genCallback(function (err, res) {
  if (err) {
    return console.error(err)
  }

  console.log(res)
  //=> ['a', {b: 'b'}, ['c', 'f'], 123, 'content of package.json',  'foobar']
  console.log(res.length)
  //=> 6
})

See aslo

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.

Charlike Make Reagent new message to charlike freenode #charlike

tunnckocore.tk keybase tunnckocore tunnckoCore npm tunnckoCore twitter tunnckoCore github