OLD | NEW |
(Empty) | |
| 1 /** |
| 2 @license |
| 3 Copyright (c) 2016 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 */ |
| 10 Promise.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 |
| 39 Promise.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 |
OLD | NEW |