• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2@license
3Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
4This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7Code distributed by Google as part of the polymer project is also
8subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9*/
10Promise.all = Promise.all || function () {
11  var args = Array.prototype.slice.call(arguments.length === 1 && Array.isArray(arguments[0]) ? arguments[0] : arguments);
12
13  return new Promise(function (resolve, reject) {
14    if (args.length === 0) return resolve([]);
15    var remaining = args.length;
16    function res(i, val) {
17      try {
18        if (val && (typeof val === 'object' || typeof val === 'function')) {
19          var then = val.then;
20          if (typeof then === 'function') {
21            then.call(val, function (val) { res(i, val) }, reject);
22            return;
23          }
24        }
25        args[i] = val;
26        if (--remaining === 0) {
27          resolve(args);
28        }
29      } catch (ex) {
30        reject(ex);
31      }
32    }
33    for (var i = 0; i < args.length; i++) {
34      res(i, args[i]);
35    }
36  });
37};
38
39Promise.race = Promise.race || function(values) {
40  // TODO(bradfordcsmith): To be consistent with the ECMAScript spec, this
41  //     method should take any iterable, not just an array.
42  var forcedArray = /** @type {!Array<!Thenable>} */ (values);
43  return new Promise(function (resolve, reject) {
44    for(var i = 0, len = forcedArray.length; i < len; i++) {
45      forcedArray[i].then(resolve, reject);
46    }
47  });
48};
49
50