A lightweight CommonJS Promises/A and when()
implementation. It also provides several other useful Promise-related concepts, such as joining and chaining, and has a robust unit test suite.
It's just over 1k when compiled with Google Closure (w/advanced optimizations) and gzipped.
when.js was derived from the async core of wire.js.
- Performance optimizations for
when.map()
(thanks @smitranic), especially for large arrays where themapFunc
is also async (i.e. returns a promise) when.all/some/any/map/reduce
handle sparse arrays (thanks @rwaldrn)- Other minor performance optimizations
v0.10.1
8000- Minor tweaks (thanks @johan)
- Add missing semis that WebStorm didn't catch
- Fix DOH submodule ref, and update README with info for running unit tests
when.map
andwhen.reduce
- just like Array.map and Array.reduce, but they operate on promises and arrays of promises- Lots of internal size and performance optimizations
- Still only 1k!
- Important fix for break in promise chains
Check the wiki for examples
Register a handler for a promise or immediate value:
when(promiseOrValue, callback, errback, progressback)
// Always returns a promise, so can be chained:
when(promiseOrValue, callback, errback, progressback).then(anotherCallback, anotherErrback, anotherProgressback)
Create a new Deferred containing separate promise
and resolver
parts:
var deferred = when.defer();
var promise = deferred.promise;
var resolver = deferred.resolver;
The deferred has the full promise
+ resolver
API:
deferred.then(callback, errback, progressback);
deferred.resolve(value);
deferred.reject(reason);
deferred.progress(update);
The promise
API:
// var promise = deferred.promise;
promise.then(callback, errback, progressback);
The resolver
API:
// var resolver = deferred.resolver;
resolver.resolve(value);
resolver.reject(err);
resolver.progress(update);
var is = when.isPromise(anything);
Return true if anything
is truthy and implements the then() promise API. Note that this will return true for both a deferred (i.e. when.defer()
), and a deferred.promise
since both implement the promise API.
when.some(promisesOrValues, howMany, callback, errback, progressback)
Return a promise that will resolve when howMany
of the supplied promisesOrValues
have resolved. The resolution value of the returned promise will be an array of length howMany
containing the resolutions values of the triggering promisesOrValues
.
when.all(promisesOrValues, callback, errback, progressback)
Return a promise that will resolve only once all the supplied promisesOrValues
have resolved. The resolution value of the returned promise will be an array containing the resolution values of each of the promisesOrValues
.
when.any(promisesOrValues, callback, errback, progressback)
Return a promise that will resolve when any one of the supplied promisesOrValues
has resolved. The resolution value of the returned promise will be the resolution value of the triggering promiseOrValue
.
when.chain(promiseOrValue, resolver, optionalValue)
Ensure that resolution of promiseOrValue
will complete resolver
with the completion value of promiseOrValue
, or instead with optionalValue
if it is provided.
Returns a new promise that will complete when promiseOrValue
is completed, with the completion value of promiseOrValue
, or instead with optionalValue
if it is provided.
Note: If promiseOrValue
is not an immediate value, it can be anything that supports the promise API (i.e. then()
), so you can pass a deferred
as well. Similarly, resolver
can be anything that supports the resolver API (i.e. resolve()
, reject()
), so a deferred
will work there, too.
when.map(promisesOrValues, mapFunc)
Traditional map function, similar to Array.prototype.map()
, but allows input to contain promises and/or values, and mapFunc may return either a value or a promise.
The map function should have the signature:
mapFunc(item)
Where:
item
is a fully resolved value of a promise or value inpromisesOrValues
when.reduce(promisesOrValues, reduceFunc, initialValue)
Traditional reduce function, similar to Array.prototype.reduce()
, but input may contain promises and/or values, and reduceFunc may return either a value or a promise, and initialValue may be a promise for the starting value.
The reduce function should have the signature:
reduceFunc(currentValue, nextItem, index, total)
Where:
currentValue
is the current accumulated reduce valuenextItem
is the fully resolved value of the promise or value atindex
inpromisesOrValues
index
the basis ofnextItem
... practically speaking, this is the array index of the promiseOrValue corresponding tonextItem
total
is the total number of items inpromisesOrValues
To run the unit tests, from the when.js
dir:
git submodule init
git submodule update
- Open test.index.html in your browser
Much of this code is based on @unscriptable's tiny promises, the async innards of wire.js, and some gists here, here, here, and here
Some of the code has been influenced by the great work in Q, Dojo's Deferred, and uber.js.