When.js is cujoJS's lightweight Promises/A+ and when()
implementation that powers the async core of wire.js, cujoJS's IOC Container. It features:
- A rock solid, battle-tested Promise implementation
- Resolving, settling, mapping, and reducing arrays of promises
- Executing tasks in parallel and sequence
- Transforming Node-style and other callback-based APIs into promise-based APIs
It passes the Promises/A+ Test Suite, is very fast and compact, and has no external dependencies.
- New
promise.done
allows consuming the ultimate value at the end of a promise chain while ensuring that any errors are thrown to the host environment so you get loud stack traces. -
when/node/function
bindCallback
andliftCallback
now behave more like standard node-style APIs in that they allow exceptions to propagate to the host environment for loud stack traces.
-
ensure
now ignores non-functions, likethen
does, for consistency. (#207)
- Promises/A+ 1.1 compliant. Passes version 2.0.0 of the Promises/A+ test suite.
- New
MutationObserver
scheduler further reduces "time-to-first-handler" in modern browsers. (#198)- Also, this works around a horrible IE10 bug (desktop and mobile) that renders
setImmediate
,MessageChannel
, andpostMessage
unusable as fast task schedulers. Many thanks to @plaa and @calvinmetcalf for their help in discovering the problem and working out a solution. (#197)
- Also, this works around a horrible IE10 bug (desktop and mobile) that renders
- Experimental support for vert.x 2.x. Should now run in vert.x >= 1.1.0.
- New
when.isPromiseLike
as the more accurately-named synonym forwhen.isPromise
. -
DEPRECATED:
when.isPromise
. It can only tell you that something is "promise-like" (aka "thenable") anyway. Use the new, more accurately-namedwhen.isPromiseLike
instead. - Fix for promise monitor reporting extra unhandled rejections for
when.all
andwhen.map
.
- New
promise.tap
for adding side effects to a promise chain. - New
MessageChannel
scheduler reduces "time-to-first" handler, in environments that support it. - Performance optimizations for promise resolution.
- Internal architecture improvements to pave the way for when.js 3.0.0.
-
Get it
-
bower install when
oryeoman install when
, or -
git clone https://github.com/cujojs/when
orgit submodule add https://github.com/cujojs/when
-
-
Configure your loader with a package:
packages: [ { name: 'when', location: 'path/to/when/', main: 'when' }, // ... other packages ... ]
-
define(['when', ...], function(when, ...) { ... });
orrequire(['when', ...], function(when, ...) { ... });
npm install when
var when = require('when');
ringo-admin install cujojs/when
var when = require('when');
-
git clone https://github.com/cujojs/when
orgit submodule add https://github.com/cujojs/when
-
Add a transient
define
shim, and a<script>
element for when.js<script> window.define = function(factory) { try{ delete window.define; } catch(e){ window.define = void 0; } // IE window.when = factory(); }; window.define.amd = {}; </script> <script src="path/to/when/when.js"></script>
-
when
will be available aswindow.when
Note that when.js includes the Promises/A+ Test Suite. Running unit tests in Node will run both when.js's own test suite, and the Promises/A+ Test Suite.
npm install
npm test
npm install
-
npm start
- starts buster server & prints a url - Point browsers at /capture, e.g.
localhost:1111/capture
npm run-script test-browser
Much of this code was inspired by the async innards of wire.js, and has been influenced by the great work in Q, Dojo's Deferred, and uber.js.