vow.Promise
A class which describes the promise objects.
Note
This class is a part of the Vow library. Only some of the methods are described below. The complete list of methods is available here: http://dfilatov.github.io/vow/. Copyright (c) 2012-2013 Filatov Dmitry (dfilatov@yandex-team.ru). Dual licensed under the MIT and GPL licenses.
Note
It is not a stand-alone module: it is available only if the vow module is connected.
Constructor
vow.Promise([resolver])
Creates a promise object.
Parameters:
Parameter |
Default value |
Description |
— |
Type: Function The function which takes the resolve and reject methods as parameters to set status and values for the created promise object. |
Example:
function someAsyncMethod () {
return new ymaps.vow.Promise(function (resolve, reject) {
doSomeAsyncStuff(function (err, value) {
if (err) {
reject(err);
return;
}
resolve(value);
});
});
}
someAsyncMethod().then(function (value) {
console.log('The method result: ' + value);
}, function (err) {
console.log('Error: ' + err);
});
Methods
Name |
Returns |
Description |
done([onFulfilled[, onRejected[, onProgress[, ctx]]]]) |
Similar to the method vow.Promise.then, which closes the promise chain. Throws an exception if the promise object is rejected. |
|
spread([onFulfilled[, onRejected[, ctx]]]) |
Similar to the method vow.Promise.then, which calls the callback functions with a set of arguments (corresponding to an array) for which the promise object can be either allowed or rejected. Usually it is used in combination with methods like vow.all. |
|
then([onFulfilled[, onRejected[, onProgress[, ctx]]]]) |
Specifies a handler function for a promise object. |
|
valueOf() |
Object |
Returns a value for an allowed promise object or a rejection reason for a rejected one. |
Methods details
done
{} done([onFulfilled[, onRejected[, onProgress[, ctx]]]])
Similar to the method vow.Promise.then, which closes the promise chain. Throws an exception if the promise object is rejected.
Parameters:
Parameter |
Default value |
Description |
— |
Type: Function The callback function which will be called if the promise object is resolved. |
|
— |
Type: Function The callback function which will be called if the promise object is rejected. |
|
— |
Type: Function The callback function which will be called when "notifying" the promise object. |
|
— |
Type: Object The execution context of the callback functions. |
Example:
var deferred = ymaps.vow.defer();
deferred.reject(Error('Internal error'));
deferred.promise().done(); // Throws an exception.
spread
{vow.Promise} spread([onFulfilled[, onRejected[, ctx]]])
Similar to the method vow.Promise.then, which calls the callback functions with a set of arguments (corresponding to an array) for which the promise object can be either allowed or rejected. Usually it is used in combination with methods like vow.all.
Returns a new promise object.
Parameters:
Parameter |
Default value |
Description |
— |
Type: Function The callback function which will be called if the promise object is resolved. |
|
— |
Type: Function The callback function which will be called if the promise object is rejected. |
|
— |
Type: Object The execution context of the callback functions. |
Example:
var deferred1 = ymaps.vow.defer();
var deferred2 = ymaps.vow.defer();
ymaps.vow.all([deferred1.promise(), deferred2.promise()]).spread(function(arg1, arg2) {
// arg1 => 1, arg2 => 'two'
});
deferred1.resolve(1);
deferred2.resolve('two');
then
{vow.Promise} then([onFulfilled[, onRejected[, onProgress[, ctx]]]])
Specifies a handler function for a promise object.
Returns a new promise object. See the specification.
Parameters:
Parameter |
Default value |
Description |
— |
Type: Function The callback function which will be called if the promise object is resolved. |
|
— |
Type: Function The callback function which will be called if the promise object is rejected. |
|
— |
Type: Function The callback function which will be called when "notifying" the promise object. |
|
— |
Type: Object The execution context of the callback functions. |
valueOf
{Object} valueOf()
Returns a value for an allowed promise object or a rejection reason for a rejected one.
Mandatory parameter/option.