]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/when.js.uncompressed.js
4 ], function(Deferred
, Promise
){
10 return function when(valueOrPromise
, callback
, errback
, progback
){
12 // Transparently applies callbacks to values and/or promises.
14 // Accepts promises but also transparently handles non-promises. If no
15 // callbacks are provided returns a promise, regardless of the initial
16 // value. Foreign promises are converted.
18 // If callbacks are provided and the initial value is not a promise,
19 // the callback is executed immediately with no error handling. Returns
20 // a promise if the initial value is a promise, or the result of the
21 // callback otherwise.
23 // Either a regular value or an object with a `then()` method that
24 // follows the Promises/A specification.
25 // callback: Function?
26 // Callback to be invoked when the promise is resolved, or a non-promise
29 // Callback to be invoked when the promise is rejected.
30 // progback: Function?
31 // Callback to be invoked when the promise emits a progress update.
32 // returns: dojo/promise/Promise
33 // Promise, or if a callback is provided, the result of the callback.
35 var receivedPromise
= valueOrPromise
&& typeof valueOrPromise
.then
=== "function";
36 var nativePromise
= receivedPromise
&& valueOrPromise
instanceof Promise
;
40 return callback(valueOrPromise
);
42 return new Deferred().resolve(valueOrPromise
);
44 }else if(!nativePromise
){
45 var deferred
= new Deferred(valueOrPromise
.cancel
);
46 valueOrPromise
.then(deferred
.resolve
, deferred
.reject
, deferred
.progress
);
47 valueOrPromise
= deferred
.promise
;
50 if(callback
|| errback
|| progback
){
51 return valueOrPromise
.then(callback
, errback
, progback
);
53 return valueOrPromise
;