Permalink
Switch branches/tags
Find file
Fetching contributors…
Cannot retrieve contributors at this time
65 lines (49 sloc) 1.17 KB

Implementation of Promises/A+

Build Status

Usage

Creating a promise

var promising = require('promising');

function myAsyncFunction() {
  var promise = promising();
  // do something...
  return promise;
}

Fulfilling a promise

function myAsyncFunction() {
  var promise = promising();
  setTimeout(function() {
    promise.fulfill(42);
  }, 2000);
  return promise;
}

Rejecting a promise

function myAsyncFunction() {
  var promise = promising();
  setTimeout(function() {
    promise.reject(24);
  }, 2000);
  return promise;
}

Most conveniently:

function myAsyncFunction() {
  return promising(function(promise) {
    promise.fulfill();
    // or
    promise.reject('foo');
    // or
    throw 'foo'; // <<< equals rejecting
  });
});

That's about it.

Who uses it?