Skip to content

Converts result-first callbacks to classic (node.js-style) error-first callbacks with 3 lines of code. Useful when you want to promisify result-first APIs (like emitter.on). Inspired by [rfpify][] and probably used in it.

License

Notifications You must be signed in to change notification settings

tunnckoCore/to-callback

Repository files navigation

Converts result-first callbacks to classic (node.js-style) error-first callbacks with 3 lines of code. Useful when you want to promisify result-first APIs (like emitter.on). Inspired by rfpify and probably used in it.

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

Table of Contents

Install

Install with npm

$ npm i to-callback --save

What is this for?

Some APIs do not use an error-first callback approach, which is convention in NodeJS land. This library converts those APIs with "result-first" callback to have "error-first" callback.

back to top

Why it exists?

Inspiration for this was rfpify and especially the wanted feature issue#3 at pify library. The @SamVerschueren take on that forks the original pify code base and andjust the needed changes. But I'm curious why? Isn't it easier to convert simply "result-first" to "error-first" and than just pass to original pify.

So that's what this package does. :)

I hope to merge it into rfpify or directly to pify so we can resolve the issue#3.

back to top

How to promisify "result-first" functions

This is example how rfpify can look like in future

var pify = require('pify')
module.exports = (fn, opts) => pify(toCallback(fn), opts)

then we can promisify "result-first" functions:

const rfpify = require('rfpify')
const EventEmitter3 = require('eventemitter3')
const emitter = new EventEmitter3()

// classic "result-first" case
emitter.on('foo', (a, b) => {
  console.log(a, b) // => 33, 55
})

// good idea is to `bind` the given function
const onPromisified = rfpify(emitter.on.bind(emitter))

onPromisified('foo')
  .then((res) => {
    console.log(res[0], res[1])
    // twice
    // => 33, 55
  })
  .catch(console.error) // => Error if something fails

emitter.emit('foo', 33, 55)
emitter.emit('foo', 33, 55)

back to top

Usage

For more use-cases see the tests

const toCallback = require('to-callback')

API

Gets a fn function, that has "result-first" callback and return same function, but with "error-first" callback, like is the convention at node.js land. Such APIs that has "result-first" callbacks are for example the eventemitter's (streams too) .on method, which should have two arguments eventName and "result-first" callback, that gets as many arguments as .emit sends.

Params

  • <fn> {Function}: function that has "result-first" callback
  • returns {Function}: function with same arguments, but instead of "result-first" callback it has "error-first" callback

Example

var EventEmitter3 = require('eventemitter3')
var toCallback = require('to-callback')
var emitter = new EventEmitter3()

// classic "result-first" case
emitter.on('foo', function (a, b) {
  console.log(a, b) // => 1, 2
})

var onWithCallback = toCallback(emitter.on.bind(emitter))

// node.js's "error-first" style
onWithCallback('foo', function (err, a, b) {
  console.log(err, a, b) // => null, 1, 2
})

emitter.emit('foo', 1, 2)

back to top

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

Converts result-first callbacks to classic (node.js-style) error-first callbacks with 3 lines of code. Useful when you want to promisify result-first APIs (like emitter.on). Inspired by [rfpify][] and probably used in it.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published