promise-ts

TypeScript promises for Node.js.

#promise-ts

Promises for Node.js, written in TypeScript and distributed in JavaScript, along with a TypeScript definition file at the root (promise.d.ts). These promise classes and functions "mostly" behave like jQuery promises, so you can refer to jQuery documentation for more detailed information.

The following classes and function are made available to you:

  1. Deferred object (jQuery doc)
  2. Promise object (jQuery doc)
  3. when function (jQuery doc)

The only known differences between the jQuery counterparts are in cases like jQuery's deferred.done, where the documentation states, "The deferred.done() method accepts one or more arguments, all of which can be either a single function or an array of functions." In this library's case, it's just a TypeScript (...callbacks: Function[]) method signature. Contributions are welcome to fix this, but I didn't have the need to go down that road myself.

///<reference path='node_modules/promise-ts/promise-ts.d.ts'/>
import promise = require('promise-ts');
var Deferred = promise.Deferred;
var when = promise.when; // not used in this example, but handy.
 
function doSomethingAsync(): promise.Promise {
    var d = new Deferred();
    setTimeout(() => {
        // time consuming operations
        d.resolve('foo');
    });
    return d.promise;
}
 
doSomethingAsync.done(result => {
    // result === 'foo';
});
var promise = require('promise-ts');
var Deferred = promise.Deferred;
var when = promise.when; // not used in this example, but handy. 
 
function doSomethingAsync() {
    var d = new Deferred();
    setTimeout(function() {
        // time consuming operations 
        d.resolve('foo');
    });
    return d.promise;
}
 
doSomethingAsync.done(function(result) {
    // result === 'foo'; 
});

See the specs for more examples. The library and specs have both been written to satisfy the jQuery usages.

MIT © Jed Mao