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
MutationObserverscheduler 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, andpostMessageunusable 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.isPromiseLikeas 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.isPromiseLikeinstead. - Fix for promise monitor reporting extra unhandled rejections for
when.allandwhen.map.
- New
promise.tapfor adding side effects to a promise chain. - New
MessageChannelscheduler 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.
- Fix for
when.defer().reject()bypassing the unhandled rejection monitor. (#166) - Fix for
when/function,when/callbacks, andwhen/node/functionnot preservingthisArg. (#162) - Doc clarifications for
promise.yield. (#164)
- New experimental promise monitoring and debugging via
when/monitor/console. - New
when.promise(resolver)promise creation API. A lighter alternative to the heavierwhen.defer() - New
bindCallbackandliftCallbackinwhen/node/functionfor more integration options with node-style callbacks.
- Quote internal usages of
promise.yieldto workaround .NET minifier tools that don't yet understand ES5 identifier-as-property rules. See #157
- New
when.settlethat settles an array of promises - New
when/guardgeneralized concurrency guarding and limiting - New
promise.inspectfor synchronously getting a snapshot of a promise's state at a particular instant. - Significant performance improvements when resolving promises with non-primitives (e.g. with Arrays, Objects, etc.)
- Experimental vert.x support
- DEPRECATED:
onFulfilled,onRejected,onProgresshandler arguments towhen.all,when.any,when.some. Use the returned promise'sthen()(orotherwise(),ensure(), etc) to register handlers instead.- For example, do this:
when.all(array).then(onFulfilled, onRejected)instead of this:when.all(array, onFulfilled, onRejected). The functionality is equivalent.
- For example, do this:
- Account for the fact that Mocha creates a global named
process. Thanks Narsul
- Fully asynchronous resolutions.
- Promises/A+ compliance.
- New
when/keysmodule withall()andmap()for object keys/values. - New
promise.ensureas a better, and safer, replacement forpromise.always. See discussion as to whypromise.alwaysis mistake-prone.- DEPRECATED:
promise.always
- DEPRECATED:
lift()is now the preferred name for what wasbind()in when/function, when/node/function, and when/callbacks.- DEPRECATED:
bind()inwhen/function,when/node/function, andwhen/callbacks. Uselift()instead.
- DEPRECATED:
-
Get it
bower install whenoryeoman install when, orgit clone https://github.com/cujojs/whenorgit 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 whenvar when = require('when');
ringo-admin install cujojs/whenvar when = require('when');
-
git clone https://github.com/cujojs/whenorgit submodule add https://github.com/cujojs/when -
Add a transient
defineshim, 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>
-
whenwill 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 installnpm test
npm installnpm 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.