Module ringo/promise

Allows to work with deferred values that will be resolved in the future.

Functions

Class Deferred

Instance Methods

Instance Properties

Class Promise

Instance Methods

  • then(callback, errback)

Deferred.prototype.promise

The promise object can be used to register a callback to be invoked when the promise is eventually resolved, or wait for the promise to be resolved.


Deferred.prototype.resolve (result, isError)

Resolve the promise.

Parameters

Object result the result or error value
boolean isError if true the promise is resolved as failed

Promise.prototype.then (callback, errback)

Invoke a callback or errback function when the promise is resolved.

Parameters

function callback called if the promise is resolved as fulfilled
function errback called if the promise is resolved as failed

Returns

Object a new promise that resolves to the return value of the callback or errback when it is called.

defer ()

Returns an object of type Deferred representing a deferred value. The deferred is a JavaScript object with two properties: a Promise object and a resolve function.

The promise object can be used to register a callback to be invoked when the promise is eventually resolved, or wait for the promise to be resolved.

The resolve function is used to resolve the promise as either fulfilled or failed.

Example

// Example for an asynchronous JSGI response.
// The response is generated after a one second delay.
exports.asyncAction = function(request) {
  var response = defer();
  setTimeout(function() {
      response.resolve({
          status: 200, headers: {}, body: ["Delayed"]
      });
  }, 1000);
  return response.promise;
}

promises (promise...)

Combine several promises passed as arguments into one. The promise returned by this function resolves to an array of objects, each containing a value or error property with the value or error of the corresponding promise. The returned promise always resolves successfully, provided all input promises are resolved.

Parameters

promise promise... any number of promises

Returns

promise a promise resolving to an array of the argument promises' values