settle.js (1366B)
1 "use strict"; 2 module.exports = 3 function(Promise, PromiseArray, debug) { 4 var PromiseInspection = Promise.PromiseInspection; 5 var util = require("./util"); 6 7 function SettledPromiseArray(values) { 8 this.constructor$(values); 9 } 10 util.inherits(SettledPromiseArray, PromiseArray); 11 12 SettledPromiseArray.prototype._promiseResolved = function (index, inspection) { 13 this._values[index] = inspection; 14 var totalResolved = ++this._totalResolved; 15 if (totalResolved >= this._length) { 16 this._resolve(this._values); 17 return true; 18 } 19 return false; 20 }; 21 22 SettledPromiseArray.prototype._promiseFulfilled = function (value, index) { 23 var ret = new PromiseInspection(); 24 ret._bitField = 33554432; 25 ret._settledValueField = value; 26 return this._promiseResolved(index, ret); 27 }; 28 SettledPromiseArray.prototype._promiseRejected = function (reason, index) { 29 var ret = new PromiseInspection(); 30 ret._bitField = 16777216; 31 ret._settledValueField = reason; 32 return this._promiseResolved(index, ret); 33 }; 34 35 Promise.settle = function (promises) { 36 debug.deprecated(".settle()", ".reflect()"); 37 return new SettledPromiseArray(promises).promise(); 38 }; 39 40 Promise.allSettled = function (promises) { 41 return new SettledPromiseArray(promises).promise(); 42 }; 43 44 Promise.prototype.settle = function () { 45 return Promise.settle(this); 46 }; 47 };