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/promise/first.js.uncompressed.js | |
parent | 70db7424e7068701e60cc5bcdfe8f858be508179 (diff) | |
parent | c670a80ddd9b03bd4ea6d940a9ed682fd26248d7 (diff) |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'lib/dojo/promise/first.js.uncompressed.js')
-rw-r--r-- | lib/dojo/promise/first.js.uncompressed.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/dojo/promise/first.js.uncompressed.js b/lib/dojo/promise/first.js.uncompressed.js new file mode 100644 index 000000000..863573fa1 --- /dev/null +++ b/lib/dojo/promise/first.js.uncompressed.js @@ -0,0 +1,49 @@ +define("dojo/promise/first", [ + "../_base/array", + "../Deferred", + "../when" +], function(array, Deferred, when){ + "use strict"; + + // module: + // dojo/promise/first + + var forEach = array.forEach; + + return function first(objectOrArray){ + // summary: + // Takes multiple promises and returns a new promise that is fulfilled + // when the first of these promises is fulfilled. + // description: + // Takes multiple promises and returns a new promise that is fulfilled + // when the first of these promises is fulfilled. Canceling the returned + // promise will *not* cancel any passed promises. The promise will be + // fulfilled with the value of the first fulfilled promise. + // objectOrArray: Object|Array? + // The promises are taken from the array or object values. If no value + // is passed, the returned promise is resolved with an undefined value. + // returns: dojo/promise/Promise + + var array; + if(objectOrArray instanceof Array){ + array = objectOrArray; + }else if(objectOrArray && typeof objectOrArray === "object"){ + array = []; + for(var key in objectOrArray){ + if(Object.hasOwnProperty.call(objectOrArray, key)){ + array.push(objectOrArray[key]); + } + } + } + + if(!array || !array.length){ + return new Deferred().resolve(); + } + + var deferred = new Deferred(); + forEach(array, function(valueOrPromise){ + when(valueOrPromise, deferred.resolve, deferred.reject); + }); + return deferred.promise; // dojo/promise/Promise + }; +}); |