to-callback
v1.0.1
Published
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.
Downloads
27
Readme
to-callback
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.
Table of Contents
- Install
- What is this for?
- Why it exists?
- How to promisify "result-first" functions
- Usage
- API
- Related
- Contributing
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.
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.
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)
Usage
For more use-cases see the tests
const toCallback = require('to-callback')
API
toCallback
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 argumentseventName
and "result-first" callback, that gets as many arguments as.emit
sends.
Params
<fn>
{Function}: function that has "result-first" callbackreturns
{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)
Related
- always-done: Handle completion and errors with elegance! Support for streams, callbacks, promises, child processes, async/await and sync functions. A drop-in replacement… more | homepage
- pify: Promisify a callback-style function | homepage
- rfpify: Promisify a result-first callback-style function. | homepage
- to-promise: Convert to promise. | homepage
- try-catch-callback: try/catch block with a callback, used in try-catch-core. Use it when you don't care about asyncness so much and don't… more | homepage
- try-catch-core: Low-level package to handle completion and errors of sync or asynchronous functions, using once and dezalgo libs. Useful for and… 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.