diff options
author | Richard Beales <[email protected]> | 2013-03-18 07:32:01 +0000 |
---|---|---|
committer | Richard Beales <[email protected]> | 2013-03-18 07:32:01 +0000 |
commit | 7c97d17aaf373339a8bcd917ad59ca6018148f0d (patch) | |
tree | 5a3c04f0f9529be392c1263d3feb75806eb43797 /lib/dojo/when.js.uncompressed.js | |
parent | 70db7424e7068701e60cc5bcdfe8f858be508179 (diff) | |
parent | c670a80ddd9b03bd4ea6d940a9ed682fd26248d7 (diff) |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'lib/dojo/when.js.uncompressed.js')
-rw-r--r-- | lib/dojo/when.js.uncompressed.js | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/dojo/when.js.uncompressed.js b/lib/dojo/when.js.uncompressed.js new file mode 100644 index 000000000..7c7764250 --- /dev/null +++ b/lib/dojo/when.js.uncompressed.js @@ -0,0 +1,55 @@ +define("dojo/when", [ + "./Deferred", + "./promise/Promise" +], function(Deferred, Promise){ + "use strict"; + + // module: + // dojo/when + + return function when(valueOrPromise, callback, errback, progback){ + // summary: + // Transparently applies callbacks to values and/or promises. + // description: + // Accepts promises but also transparently handles non-promises. If no + // callbacks are provided returns a promise, regardless of the initial + // value. Foreign promises are converted. + // + // If callbacks are provided and the initial value is not a promise, + // the callback is executed immediately with no error handling. Returns + // a promise if the initial value is a promise, or the result of the + // callback otherwise. + // valueOrPromise: + // Either a regular value or an object with a `then()` method that + // follows the Promises/A specification. + // callback: Function? + // Callback to be invoked when the promise is resolved, or a non-promise + // is received. + // errback: Function? + // Callback to be invoked when the promise is rejected. + // progback: Function? + // Callback to be invoked when the promise emits a progress update. + // returns: dojo/promise/Promise + // Promise, or if a callback is provided, the result of the callback. + + var receivedPromise = valueOrPromise && typeof valueOrPromise.then === "function"; + var nativePromise = receivedPromise && valueOrPromise instanceof Promise; + + if(!receivedPromise){ + if(callback){ + return callback(valueOrPromise); + }else{ + return new Deferred().resolve(valueOrPromise); + } + }else if(!nativePromise){ + var deferred = new Deferred(valueOrPromise.cancel); + valueOrPromise.then(deferred.resolve, deferred.reject, deferred.progress); + valueOrPromise = deferred.promise; + } + + if(callback || errback || progback){ + return valueOrPromise.then(callback, errback, progback); + } + return valueOrPromise; + }; +}); |