JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1198029
  • Score
    100M100P100Q197112F
  • License MIT

Library to help write libraries that accept both promises and callbacks.

Package Exports

  • promise-breaker

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (promise-breaker) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Build Status Coverage Status

What is it?

promise-breaker makes it easy to write functions that will accept an optional callback, or return a Promise if a callback is not provided. You can use callbacks or Promises in your implementation, and callers can call with either a callback or expect a Promise. It's a library that makes it easy to write libraries for others.

Installation

npm install --save promise-breaker

Requirements

This library assumes that Promise is a defined global variable. If this is not the case on your platform, you can use a polyfill:

npm install --save es6-promise

Then somewhere in your node.js application:

if(!global.Promise) {
    global.Promise = require('es6-promise').Promise;
}

Or in your client-side app:

if(!window.Promise) {
    window.Promise = require('es6-promise').Promise;
}

If you don't want to set the global, you can pass an optional Promise implementation to promise-breaker:

var MyPromise = require('es6-promise').Promise;
promiseBreaker = require('promise-breaker').withPromise(MyPromise);

Summary

With the growing popularity of Promises these days, if you're a library author, it's nice to be able to provide your clients with a library that will take an optional callback, and if the callback isn't provided, return a Promise. If you've ever tried to do this, you know that there's a lot of finicky boilerplate involved in every function you write. Providing callback support is also pretty important if you prefer to write your library using Promises internally.

'promise-breaker' makes this really easy. If you prefer writing in callback style:

// We're going to make some promises from callbacks
var pb = require('promise-breaker');

exports.myFunc = pb.make(function(done) {
    done(null, "Hello World");
});

or if you prefer Promise style:

// We're going to break some promises down into callbacks
var pb = require('promise-breaker')

exports.myFunc = pb.break(function() {
    Promise.resolve("Hello World");
});

No matter which approach you take, users of your library can now call myFunc(done), or they can call myFunc().then(...).

API

pb.make(fn)

make(fn) takes a function which accepts a callback(err, result) as its last parameter, and returns a new function which accepts an optional callback as its last parameter. If a callback is provided, this new function will behave exactly like the original function. If the callback is not provided, then the new function will return a Promise.

pb.break(fn)

break(fn) is the opposite of make(fn). fn here is a function which returns a Promise. break(fn) will generate a new function with an extra parameter, an optional callback(err, result). If no callback is provided, the generated function will behave exactly like the original function. If a callback is provided, then the generated function will return null, and will pass any results that would have been returned via the Promise via the callback instead.

pb.applyFn(fn, argumentCount, thisArg, args[, cb])

Much like Function.prototype.apply(), this calls a function, but this lets you call into a function when you don't know whether the function is expecting a callback or is going to return a Promise. fn is the function you wish to call, argumentCount is the number of arguments you expect the function to take (not including the callback). Under the hood, this will call fn and pass in a callback as the arugmentCount + 1th parameter. If a Promise is returned, applyFn will assume fn is Promise based, otherwise applyFn will wait for the callback to be called.

If cb is provided, applyFn will call into cb with a result, otherwise applyFn will itself return a Promise.

pb.callFn(fn, argumentCount, thisArg[, arg1[, arg2[, ...[, cb]]]])

This is the Function.prototype.call() equivalent of applyFn().

pb.withPromise(promiseImpl)

Returns a new {make, break, applyFn, callFn} object which uses the specified promiseImpl constructor to create new Promises.