]> git.wh0rd.org Git - tt-rss.git/blob - lib/dojo/promise/first.js.uncompressed.js
upgrade dojo to 1.8.3 (refs #570)
[tt-rss.git] / lib / dojo / promise / first.js.uncompressed.js
1 define("dojo/promise/first", [
2         "../_base/array",
3         "../Deferred",
4         "../when"
5 ], function(array, Deferred, when){
6         "use strict";
7
8         // module:
9         //              dojo/promise/first
10
11         var forEach = array.forEach;
12
13         return function first(objectOrArray){
14                 // summary:
15                 //              Takes multiple promises and returns a new promise that is fulfilled
16                 //              when the first of these promises is fulfilled.
17                 // description:
18                 //              Takes multiple promises and returns a new promise that is fulfilled
19                 //              when the first of these promises is fulfilled. Canceling the returned
20                 //              promise will *not* cancel any passed promises. The promise will be
21                 //              fulfilled with the value of the first fulfilled promise.
22                 // objectOrArray: Object|Array?
23                 //              The promises are taken from the array or object values. If no value
24                 //              is passed, the returned promise is resolved with an undefined value.
25                 // returns: dojo/promise/Promise
26
27                 var array;
28                 if(objectOrArray instanceof Array){
29                         array = objectOrArray;
30                 }else if(objectOrArray && typeof objectOrArray === "object"){
31                         array = [];
32                         for(var key in objectOrArray){
33                                 if(Object.hasOwnProperty.call(objectOrArray, key)){
34                                         array.push(objectOrArray[key]);
35                                 }
36                         }
37                 }
38
39                 if(!array || !array.length){
40                         return new Deferred().resolve();
41                 }
42
43                 var deferred = new Deferred();
44                 forEach(array, function(valueOrPromise){
45                         when(valueOrPromise, deferred.resolve, deferred.reject);
46                 });
47                 return deferred.promise;        // dojo/promise/Promise
48         };
49 });