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

promisebacker

v0.1.2

Published

A callback-to-promise-and-back adapter.

Downloads

37

Readme

Promisebacker

Wraps an asynchronous function that either takes a callback or returns a promise, and allows it to do both.

Note: Promisebacker is still under development and not yet ready for production systems. Releases are stable but the API is subject to rapidly change.

Installation

npm install promisebacker

Example

var Promise = require('promise') // Any Promises/A+ compliant library will do.
  , promisebacker = require('promisebacker');

var takes_callback = function(arg1, arg2, callback) {
  // Does something...
  var error = false;

  // Asynchronously invokes callback.
  setTimeout(function() {
    if (error){
      return callback('uh oh!');
    }
    callback(null, 'hello world!');
  }, 3000);
};

var returns_promise = function(arg1, arg2) {
  // Does something...
  var error = false;

  // Returns promise that resolves asynchronously.
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      if (error) {
        return reject('uh oh!');
      }
      resolve('hello world!');
    }, 3000);
  });
};

var wrapped_callback = promisebacker(takes_callback);

// Now we can pretend this returns a promise...
wrapped_callback('alas', 'poor yorick')
  .then(function(result) {
    // result == 'hello world!'
  });

// ...or we can continue using it as a callback!
wrapped_callback('alas', 'poor yorick', function(err, result) {
  // err == null
  // result == 'hello world!'
});

// And we can do the same for functions that return promises.
var wrapped_promise = promisebacker(returns_promise);

wrapped_promise('alas', 'poor yorick')
  .then(function(result) {
    // result == 'hello world!'
  });

wrapped_promise('alas', 'poor yorick', function(err, result) {
  // err == null
  // result == 'hello world!'
});

Usage Notes

Promisebacker(Function target) assumes that you're trying to use a callback if the last argument passed is a Function of arity at least 2. If you want to force it to return promises, use Promisebacker.toPromise instead.

API Reference

We define a function to take a node-style callback (a nodeback) if it accepts a Function of arity at least 2 as its last argument and invokes that function whenever it finishes running. When invoking its callback, it must pass an error value as its first argument which must be truthy if and only if an error has occurred.

Promisebacker(Function target [, Object options]) -> Function

Wraps target such that it can either take a callback or return a promise.

  • target must either return a promise or take nodebacks.
  • options is an optional object with options.

If target takes nodebacks and calls its callback with multiple success values, the fulfillment value will be an array of them.

See the bluebird documentation for promisification for details.

Option: Object scope (default: N/A)

If you pass a scope, then target will have its this bound to scope (i.e. as if it were being called as scope.target).

Option: Boolean spread (default: false if nodeback is of arity at most 2, true otherwise)

Some nodebacks expect more than 1 success value but there is no mapping for this in the promise world. If spread is specified, the nodeback is called with multiple values when the promise fulfillment value is an array:

var example = Promisebacker(Promise.resolve);
example([1, 2, 3], function(err, result) {
  // err == null
  // result == [1, 2, 3]
});

var another = Promisebacker(Promise.resolve, {spread: true});
another([1, 2, 3], function(err, a, b, c) {
  // err == null
  // a == 1, b == 2, c == 3
});
Promisebacker.toPromise(Function target [, Object options]) -> Function

Same as above, but will always return a Promise even if the last argument is a Function of arity at least 2.