Skip to content
/ when Public
forked from cujojs/when

A lightweight Promise and when() implementation, plus other async goodies.

License

Notifications You must be signed in to change notification settings

wycats/when

 
 

Repository files navigation

when.js Build Status

A lightweight CommonJS Promises/A and when() implementation. It also provides several other useful Promise-related concepts, such as joining, mapping and reducing, and has a robust unit test suite.

It's under 1.3k when compiled with Google Closure (w/advanced optimizations) and gzipped, and has no dependencies.

when.js was derived from the async core of wire.js.

What's New?

1.5.1

  • Performance optimization for when.defer, up to 1.5x in some cases.
  • when/debug can now log exceptions and rejections in deeper promise chains, in some cases, even when the promises involved aren't when.js promises.

1.5.0

  • New task execution and concurrency management: when/sequence, when/pipeline, and when/parallel.
  • Performance optimizations for when.all and when.map, up to 2x in some cases.
  • Options for disabling paranoid mode that provides a significant performance gain in v8 (e.g. Node and Chrome). See this v8 performance problem with Object.freeze for more info.
  • Important: deferred and deferred.resolver no longer throw when resolved/rejected multiple times. They will return silently as if the they had succeeded. This prevents parties to whom only the resolver has been given from using try/catch to determine the state of the associated promise.
    • For debugging, you can use the when/debug module, which will still throw when a deferred is resolved/rejected multiple times.

1.4.4

  • Change UMD boilerplate to check for exports to avoid a problem with QUnit. See #54 for more info.

Full Changelog

Docs & Examples

API docs

More info on the wiki

Examples

Quick Start

AMD

  1. git clone https://github.com/cujojs/when or git submodule add https://github.com/cujojs/when

  2. Configure your loader with a package:

    packages: [
    	{ name: 'when', location: 'path/to/when/', main: 'when' },
    	// ... other packages ...
    ]
  3. define(['when', ...], function(when, ...) { ... }); or require(['when', ...], function(when, ...) { ... });

Script Tag

  1. git clone https://github.com/cujojs/when or git submodule add https://github.com/cujojs/when
  2. <script src="path/to/when/when.js"></script>
  3. when will be available as window.when

Node

  1. npm install when
  2. var when = require('when');

RingoJS

  1. ringo-admin install cujojs/when
  2. var when = require('when');

Running the Unit Tests

Install buster.js

npm install -g buster

Run unit tests in Node:

  1. buster test -e node

Run unit tests in Browsers (and Node):

  1. buster server - this will print a url
  2. Point browsers at /capture, e.g. localhost:1111/capture
  3. buster test or buster test -e browser

References

Much of this code was inspired by @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.

About

A lightweight Promise and when() implementation, plus other async goodies.

Resources

License

Stars

Watchers

Forks

Packages

No packages published