Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

promiseback

ljharb433.5kMIT2.0.3

Accept an optional node-style callback, and also return a spec-compliant Promise!

promiseback, promisebacks, promise, promises, callback, callbacks

readme

promiseback Version Badge

Build Status dependency status dev dependency status

npm badge

browser support

Accept an optional node-style callback, and also return a spec-compliant Promise!

API

var promiseback = require('promiseback');
var callback = function (err, value) {};

/* without a promise: */
promiseback(callback);
    /*
        - will throw if `callback` is not a function
        - returns a "deferred"
        - has resolve/reject methods, and `promise` property
        - will call `callback` as expected when deferred is resolved
    */

/* with a promise: */
promiseback(promise, callback);
    /*
        - will throw if `callback` is truthy and not a function
        - `promise` will be converted to a Promise, so you can pass a value as well
        - returns a Promise
        - will call `callback` as expected when the promise is fulfilled
    */

Examples

Using deferreds:

var promiseback = require('promiseback');

module.exports = function doSomethingCool(thing, callback) {
    // If callback is not provided, this code will simply return a normal promise.
    // If callback is provided but is not a function, promiseback will immediately throw a TypeError.

    // "deferred" is an object with `reject/resolve` methods, and a `promise` property.
    var deferred = promiseback(callback);
    if (thing) {
        deferred.resolve(thing);
    } else {
        deferred.reject(thing);
    }
    return deferred.promise;
};

Using a straight promise, when you can get it from somewhere else:

var promiseback = require('promiseback');

module.exports = function doSomethingCool(thing, callback) {
    // If callback is not provided, this code will simply return a normal promise.
    // If callback is provided but is not a function, promiseback will immediately throw a TypeError.

    var newThingPromise = makeNewThing(thing);
    return promiseback(newThingPromise, callback);
};

Tests

Simply clone the repo, npm install, and run npm test

changelog

2.0.3 / 2020-01-14

  • [Fix] do not depend on non-standard promise-deferred extensions
  • [Deps] update is-callable
  • [Dev Deps] update eslint, @ljharb/eslint-config, tape, covert; add safe-publish-latest
  • [Tests] use shared travis-ci configs
  • [Tests] use npx aud instead of nsp or npm audit with hoops
  • [Tests] remove jscs
  • [meta] Only apps should have lockfiles
  • [meta] add funding field
  • [actions] add automatic rebasing / merge commit blocking

2.0.2 / 2015-10-14

  • Lots of code cleanup.
  • [Fix] Use is-callable to detect if the callback is callable
  • [Docs] add a CHANGELOG
  • [Docs] Switch from vb.teelaun.ch to versionbadg.es for the npm version badge SVG
  • [Tests] up to io.js v3.3, node v4.2
  • [Tests] add npm run security
  • [Dev Deps] Update tape, jscs, covert, eslint, @ljharb/eslint-config

2.0.1 / 2015-01-20

  • [Tests] Fix tests checking for wrong error type.
  • [Deps] Update promise-deferred
  • [Dev Deps] Update tape, jscs
  • [Dev Deps] Lock covert to v1.0.0.
  • [Tests] Using my standard jscs.json file
  • [Docs] Naming npm version badge in README
  • [Docs] Naming testling-ci URLs in README
  • [Docs] Naming deps/devDeps URLs in README
  • [Docs] Naming travis-ci URLs in README
  • [Docs] Naming npm version SVG in README
  • [Docs] Naming npm package URL in README

2.0.0 / 2014-09-13

  • [Dev Deps] update promise-deferred, jscs

1.0.2 / 2014-08-28

  • [Deps] update promise-deferred

1.0.1 / 2014-08-28

  • [Refactor] Using a cleaner isFunction check
  • [Docs] Use SVG badges instead of PNG
  • [Tests] Run linter as part of tests.
  • [Tests] Adding npm run lint
  • [Tests] Run code coverage checks during tests
  • [Dev Deps] update covert

1.0.0 / 2014-08-10

  • [Deps] update promise-deferred
  • [Dev Deps] update tape

0.3.0 / 2014-04-29

  • [Deps] update promise-deferred
  • [Dev Deps] update covert, tape
  • [Tests] Make sure old and unstable nodes don't break Travis

0.2.0 / 2014-03-16

  • [Deps] update promise-deferred
  • [Dev Deps] update tape, covert
  • [Tests] Testing down to node 0.4
  • [Fix] ES3 browsers don't have forEach :-(

0.1.0 / 2013-12-30

  • Use promise-deferred instead of q
  • Tests.
  • Travis CI
  • Implementation.
  • README
  • package.json
  • Initial commit