Dual licensed under the MIT and GPL licenses:
The Deferred class is used to encapsulate newly-created promise object along with functions that resolve, reject or notify it.
You can use vow.defer() instead of using this constructor.
new vow.Deferred() gives the same result as vow.defer().
Returns the corresponding promise.
Resolves the corresponding promise with the given value.
var defer = vow.defer(),
promise = defer.promise();
promise.then(function(value) {
// value is "'success'" here
});
defer.resolve('success');
Rejects the corresponding promise with the given reason.
var defer = vow.defer(),
promise = defer.promise();
promise.fail(function(reason) {
// reason is "'something is wrong'" here
});
defer.reject('something is wrong');
Notifies the corresponding promise with the given value.
var defer = vow.defer(),
promise = defer.promise();
promise.progress(function(value) {
// value is "'20%'", "'40%'" here
});
defer.notify('20%');
defer.notify('40%');
The Promise class is used when you want to give to the caller something to subscribe to,
but not the ability to resolve or reject the deferred.
You should use this constructor directly only if you are going to use vow as DOM Promises implementation.
In other case you should use vow.defer() and defer.promise() methods.
function fetchJSON(url) {
return new vow.Promise(function(resolve, reject, notify) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'json';
xhr.send();
xhr.onload = function() {
if(xhr.response) {
resolve(xhr.response);
}
else {
reject(new TypeError());
}
};
});
}
Returns the value of the fulfilled promise or the reason in case of rejection.
Returns true if the promise is resolved.
Returns true if the promise is fulfilled.
Returns true if the promise is rejected.
Adds reactions to the promise.
Callback that will be invoked with a provided value after the promise has been fulfilled
Callback that will be invoked with a provided reason after the promise has been rejected
Callback that will be invoked with a provided value after the promise has been notified
Context of the callbacks execution
A new promise, see https://github.com/promises-aplus/promises-spec for details
Adds only a rejection reaction. This method is a shorthand for promise.then(undefined, onRejected).
Callback that will be called with a provided 'reason' as argument after the promise has been rejected
Context of the callback execution
Adds only a rejection reaction. This method is a shorthand for promise.then(null, onRejected). It's also an alias for catch.
Callback to be called with the value after promise has been rejected
Context of the callback execution
Adds a resolving reaction (for both fulfillment and rejection).
Callback that will be invoked with the promise as an argument, after the promise has been resolved.
Context of the callback execution
Adds a resolving reaction (for both fulfillment and rejection). The returned promise will be fullfiled with the same value or rejected with the same reason as the original promise.
Callback that will be invoked after the promise has been resolved.
Context of the callback execution
Adds a progress reaction.
Callback that will be called with a provided value when the promise has been notified
Context of the callback execution
Like promise.then, but "spreads" the array into a variadic value handler.
It is useful with the vow.all and the vow.allResolved methods.
Callback that will be invoked with a provided value after the promise has been fulfilled
Callback that will be invoked with a provided reason after the promise has been rejected
Context of the callbacks execution
var defer1 = vow.defer(),
defer2 = vow.defer();
vow.all([defer1.promise(), defer2.promise()]).spread(function(arg1, arg2) {
// arg1 is "1", arg2 is "'two'" here
});
defer1.resolve(1);
defer2.resolve('two');
Like then, but terminates a chain of promises.
If the promise has been rejected, this method throws it's "reason" as an exception in a future turn of the event loop.
Callback that will be invoked with a provided value after the promise has been fulfilled
Callback that will be invoked with a provided reason after the promise has been rejected
Callback that will be invoked with a provided value after the promise has been notified
Context of the callbacks execution
var defer = vow.defer();
defer.reject(Error('Internal error'));
defer.promise().done(); // exception to be thrown
Returns a new promise that will be fulfilled in delay milliseconds if the promise is fulfilled,
or immediately rejected if the promise is rejected.
Returns a new promise that will be rejected in timeout milliseconds
if the promise is not resolved beforehand.
var defer = vow.defer(),
promiseWithTimeout1 = defer.promise().timeout(50),
promiseWithTimeout2 = defer.promise().timeout(200);
setTimeout(
function() {
defer.resolve('ok');
},
100);
promiseWithTimeout1.fail(function(reason) {
// promiseWithTimeout to be rejected in 50ms
});
promiseWithTimeout2.then(function(value) {
// promiseWithTimeout to be fulfilled with "'ok'" value
});
Coerces the given value to a promise, or returns the value if it's already a promise.
Returns a promise, that will be fulfilled only after all the items in iterable are fulfilled.
If any of the iterable items gets rejected, then the returned promise will be rejected.
Returns a promise, that will be fulfilled only when any of the items in iterable are fulfilled.
If any of the iterable items gets rejected, then the returned promise will be rejected.
Returns a promise that has already been resolved with the given value.
If value is a promise, the returned promise will have value's state.
Returns a promise that has already been rejected with the given reason.
Creates a new deferred. This method is a factory method for vow:Deferred class.
It's equivalent to new vow.Deferred().
Static equivalent to promise.then.
If value is not a promise, then value is treated as a fulfilled promise.
Callback that will be invoked with a provided value after the promise has been fulfilled
Callback that will be invoked with a provided reason after the promise has been rejected
Callback that will be invoked with a provided value after the promise has been notified
Context of the callbacks execution
Static equivalent to promise.fail.
If value is not a promise, then value is treated as a fulfilled promise.
Callback that will be invoked with a provided reason after the promise has been rejected
Context of the callback execution
Static equivalent to promise.always.
If value is not a promise, then value is treated as a fulfilled promise.
Callback that will be invoked with the promise as an argument, after the promise has been resolved.
Context of the callback execution
Static equivalent to promise.progress.
If value is not a promise, then value is treated as a fulfilled promise.
Callback that will be invoked with a provided value after the promise has been notified
Context of the callback execution
Static equivalent to promise.spread.
If value is not a promise, then value is treated as a fulfilled promise.
Callback that will be invoked with a provided value after the promise has been fulfilled
Callback that will be invoked with a provided reason after the promise has been rejected
Context of the callbacks execution
Static equivalent to promise.done.
If value is not a promise, then value is treated as a fulfilled promise.
Callback that will be invoked with a provided value after the promise has been fulfilled
Callback that will be invoked with a provided reason after the promise has been rejected
Callback that will be invoked with a provided value after the promise has been notified
Context of the callbacks execution
Checks whether the given value is a promise-like object
vow.isPromise('something'); // returns false
vow.isPromise(vow.defer().promise()); // returns true
vow.isPromise({ then : function() { }); // returns true
Coerces the given value to a promise, or returns the value if it's already a promise.
Static equivalent to promise.valueOf.
If value is not a promise, then value is treated as a fulfilled promise.
Static equivalent to promise.isFulfilled.
If value is not a promise, then value is treated as a fulfilled promise.
Static equivalent to promise.isRejected.
If value is not a promise, then value is treated as a fulfilled promise.
Static equivalent to promise.isResolved.
If value is not a promise, then value is treated as a fulfilled promise.
Returns a promise that has already been resolved with the given value.
If value is a promise, the returned promise will have value's state.
Returns a promise that has already been fulfilled with the given value.
If value is a promise, the returned promise will be fulfilled with the fulfill/rejection value of value.
Returns a promise that has already been rejected with the given reason.
If reason is a promise, the returned promise will be rejected with the fulfill/rejection value of reason.
Invokes the given function fn with arguments args
var promise1 = vow.invoke(function(value) {
return value;
}, 'ok'),
promise2 = vow.invoke(function() {
throw Error();
});
promise1.isFulfilled(); // true
promise1.valueOf(); // 'ok'
promise2.isRejected(); // true
promise2.valueOf(); // instance of Error
Returns a promise, that will be fulfilled only after all the items in iterable are fulfilled.
If any of the iterable items gets rejected, the promise will be rejected.
with array:
var defer1 = vow.defer(),
defer2 = vow.defer();
vow.all([defer1.promise(), defer2.promise(), 3])
.then(function(value) {
// value is "[1, 2, 3]" here
});
defer1.resolve(1);
defer2.resolve(2);
with object:
var defer1 = vow.defer(),
defer2 = vow.defer();
vow.all({ p1 : defer1.promise(), p2 : defer2.promise(), p3 : 3 })
.then(function(value) {
// value is "{ p1 : 1, p2 : 2, p3 : 3 }" here
});
defer1.resolve(1);
defer2.resolve(2);
Returns a promise, that will be fulfilled only after all the items in iterable are resolved.
var defer1 = vow.defer(),
defer2 = vow.defer();
vow.allResolved([defer1.promise(), defer2.promise()]).spread(function(promise1, promise2) {
promise1.isRejected(); // returns true
promise1.valueOf(); // returns "'error'"
promise2.isFulfilled(); // returns true
promise2.valueOf(); // returns "'ok'"
});
defer1.reject('error');
defer2.resolve('ok');
Returns a promise, that will be fulfilled if any of the items in iterable is fulfilled.
If all of the iterable items get rejected, the promise will be rejected (with the reason of the first rejected item).
Returns a promise, that will be fulfilled only when any of the items in iterable is fulfilled.
If any of the iterable items gets rejected, the promise will be rejected.
Static equivalent to promise.delay.
If value is not a promise, then value is treated as a fulfilled promise.
Static equivalent to promise.timeout.
If value is not a promise, then value is treated as a fulfilled promise.