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

promback

v1.0.1

Published

Level promises/callbacks landscape

Downloads

6

Readme

Build Status Coverage Status

promback

Wraps a promise-returning/callback-calling function and returns another promise-returning one.

Especially useful for api or library writers, where an async (i.e. callback-calling or promise-returning) function is expected, to wrap it and turn into a promise-returning one. This simplifies implimenting the api and lets the users of the api to either use a callback (passed by promback along with the api-dependend params) or return a promise to their liking and convenience and, simultaneously, on the api side lets you to only work with promises.

Install

npm i --save promback

Usage


const promback = require('promback')

const prombacked = promback(/* a function */)

How it is useful for library writers

Suppose that your library has a method that expects an async function from your users:

const mylib = require('mylib')

mylib.passMeSomethingAsync(/* something async */)

It calls the async function with some library-specific arguments and expects some library-expected value to be returned. So, suppose for the purposes of the example that the arguments are a and b and the value is x. promback allows you to effortlessly provide your users with the support of any asynchronicity mechanisms to their liking and convenience, be it callbacks:

mylib.passMeSomethingAsync(function (a, b, cb) {
    getXWithCallbackBecauseIAmAQualifiedUserAndCanDoIt(a, b, function (err, x) {
        cb(err, x)
    })
})

, promises:

mylib.passMeSomethingAsync(function (a, b) {
    return getXWith___WellActuallyIHaveAPromiseForThat(a, b)
    .then(function (x) {
        return x
    })
})

or even async functions:

mylib.passMeSomethingAsync(async function (a, b) {
    const x = await pfff___PromisesAreSoLastCentury(a, b)
    return x
})

Note, that in the above examples the funky functions may be passed directly to the passMeSomethingAsync. It is shown in more details just for sake of illustration.

The only thing needed to be done in passMeSomethingAsync is wrapping the async function with promback:

// deep inside mylib
mylib.passMeSomethingAsync = async function (getX) {
    getX = promback(getX)
    // get a and b from somewhere
    const x = await getX(a, b)
    // use x somehow
}

And now you don't have to think on how to handle all this stuff anymore!

How it is different from a promisify

Different flavors of promisify does not handle promise-returning functions well and is going to hang when you will try to call a promisifed promise-returning function. promback on the other hand is designed to handle both promises and callbacks.

API

promback (Function fn) -> async Function

Wraps passed function and returns prombacked one. The prombacked function passes its arguments to the wrapped one and returns a promise fulfilled on one of the following cases:

  • wrapped function called callback added to the arguments,
  • a promise returned by the wrapped function is fulfilled,
  • wrapped function returned a value immediately,
  • wrapped function thrown an error.

If the number of arguments of a prombacked function call does not match to the number of arguments expected by the wrapped function - 1, then callback is not passed to the wrapped function and its returned value resolved directly. This is done so to prevent passing an unexpected argument.

The passed callback signature is the normal node-style callback (err, value).

Prombacked function's this reference left not bound to any particular object and is still dynamic.

promback.using (PromiseLib) -> promback

By default, promback module uses the Promise defined globally. With using one able to create other instances of promback that will use specified PromiseLib to create the promise returned from prombacked function. Please note, that default promback will still use global Promise.

An example:

const bluebird = require('bluebird')
const promback = require('promback').using(bluebird)

// timeout returns a bluebird promise now
const timeout = promback(function (ms, cb) {
    setTimeout(cb, ms)
})