promise.coffee

Promises/A+ compliant implementation in object-oriented CoffeeScript

promise.coffee is an ultra-lean (~80 lines) object-oriented CoffeeScript implementation of the Promises/A+ Specification that passes the Promises/A+ Compliance Test Suite.

Inspired by avow.js, promise.coffee differs by providing a reference implementation that decomposes Promise/A+ functionality into object-oriented classes. The approach demonstrated here offers a model for how Promises might be implemented in stricter class oriented languages.

It is fully asynchronous, ensuring that the onFulfilled and onRejected callbacks are not executed in the same turn of the event loop as the call to then() with which they were registered. To schedule execution of these callbacks, it uses process.nextTick() or setImmediate() if available (see NobleJS's setImmediate() polyfill) and will otherwise fall back to setTimeout().

It supports foreign promises returned by callbacks as long as they support the standard Promise then() method signature.

Create a deferred:

var deferred = new Deferred();

Resolve that deferred:

deferred.resolve( value );

Or, reject that deferred:

deferred.reject( reason );

Obtain the promise linked to that deferred to pass to external consumers:

var promise = deferred.promise;

Add (optional) handlers to that promise:

promise.then( onFulfilled, onRejected );

Download, clone or:

npm install git://github.com/CodeCatalyst/promise.coffee.git

and then:

var Deferred = require( 'promise.coffee' ).Deferred;

Assuming you have configured your loader to map the promise.coffee files to the 'promise' module identifier.

define( function ( require, exports, module ) {
    var Deferred = require( 'promise' ).Deferred;
    
    ...
});

Reference the appropriate script file from your HTML file:

<script src="promise.js"></script>

or, for the minified version of the script:

<script src="promise.min.js"></script>

or, in the unlikely case you are using the browser-based CoffeeScript compiler:

<script type="text/coffeescript" src="promise.coffee"></script>

This implementation decomposes Promise functionality into three classes:

Promises represent a future value; i.e., a value that may not yet be available.

A Promise's then() method is used to specify onFulfilled and onRejected callbacks that will be notified when the future value becomes available. Those callbacks can subsequently transform the value that was resolved or the reason that was rejected. Each call to then() returns a new Promise of that transformed value; i.e., a Promise that is resolved with the callback return value or rejected with any error thrown by the callback.

A Deferred is typically used within the body of a function that performs an asynchronous operation. When that operation succeeds, the Deferred should be resolved; if that operation fails, the Deferred should be rejected.

Once a Deferred has been resolved or rejected, it is considered to be complete and subsequent calls to resolve() or reject() are ignored.

Deferreds are the mechanism used to create new Promises. A Deferred has a single associated Promise that can be safely returned to external consumers to ensure they do not interfere with the resolution or rejection of the deferred operation.

Resolvers are used internally by Deferreds and Promises to capture and notify callbacks, process callback return values and propogate resolution or rejection to chained Resolvers.

Developers never directly interact with a Resolver.

A Resolver captures a pair of optional onResolved and onRejected callbacks and has an associated Promise. That Promise delegates its then() calls to the Resolver's then() method, which creates a new Resolver and schedules its delayed addition as a chained Resolver.

Each Deferred has an associated Resolver. A Deferred delegates resolve() and reject() calls to that Resolver's resolve() and reject() methods. The Resolver processes the resolution value and rejection reason, and propogates the processed resolution value or rejection reason to any chained Resolvers it may have created in response to then() calls. Once a chained Resolver has been notified, it is cleared out of the set of chained Resolvers and will not be notified again.

  1. npm install
  2. npm test

Copyright (c) 2012-2013 CodeCatalyst, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.