promiseback
Accept an optional node-style callback, and also return a spec-compliant Promise!
API
var promiseback = ;var {}; /* without a promise: */; /* - 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: */; /* - 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 = ; module { // 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 = ; if thing deferred; else deferred; return deferredpromise;};
Using a straight promise, when you can get it from somewhere else:
var promiseback = ; module { // 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 = ; return ;};
Tests
Simply clone the repo, npm install
, and run npm test