promiseback
Accept an optional node-style callback, and also return a spec-compliant Promise!
Last updated 10 years ago by ljharb .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install promiseback 
SYNC missed versions from official npm registry.

#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

Current Tags

  • 2.0.3                                ...           latest (5 years ago)

10 Versions

  • 2.0.3                                ...           5 years ago
  • 2.0.2                                ...           9 years ago
  • 2.0.1                                ...           10 years ago
  • 2.0.0                                ...           10 years ago
  • 1.0.2                                ...           10 years ago
  • 1.0.1                                ...           10 years ago
  • 1.0.0                                ...           10 years ago
  • 0.3.0                                ...           11 years ago
  • 0.2.0                                ...           11 years ago
  • 0.1.0                                ...           11 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (3)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE