Module ringo/promise
Allows to work with deferred values that will be resolved in the future.
Deferred.prototype.promise
The promise object can be used to [register a callback][#deferred.promise.then] to be invoked when the promise is eventually resolved, or [wait for the promise to be resolved][#deferred.promise.wait].
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. |
Promise.prototype.wait (timeout)
Wait for the promise to be resolved.
Parameters
| number | timeout | optional time in milliseconds to wait for. If undefined wait() blocks forever. |
Returns
| Object | the value if the promise is resolved as fulfilled |
Throws
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][#deferred.promise.then] to be invoked when the promise is eventually resolved, or [wait for the promise to be resolved][#deferred.promise.wait].
The [resolve][#deferred.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 |
