npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

18

Readme

to-callback npmjs.com The MIT License npm downloads

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

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 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.

Charlike Make Reagent new message to charlike freenode #charlike

tunnckoCore.tk keybase tunnckoCore tunnckoCore npm tunnckoCore twitter tunnckoCore github