settle.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. module.exports =
  3. function(Promise, PromiseArray) {
  4. var PromiseInspection = Promise.PromiseInspection;
  5. var util = require("./util.js");
  6. function SettledPromiseArray(values) {
  7. this.constructor$(values);
  8. }
  9. util.inherits(SettledPromiseArray, PromiseArray);
  10. SettledPromiseArray.prototype._promiseResolved = function (index, inspection) {
  11. this._values[index] = inspection;
  12. var totalResolved = ++this._totalResolved;
  13. if (totalResolved >= this._length) {
  14. this._resolve(this._values);
  15. }
  16. };
  17. SettledPromiseArray.prototype._promiseFulfilled = function (value, index) {
  18. var ret = new PromiseInspection();
  19. ret._bitField = 268435456;
  20. ret._settledValue = value;
  21. this._promiseResolved(index, ret);
  22. };
  23. SettledPromiseArray.prototype._promiseRejected = function (reason, index) {
  24. var ret = new PromiseInspection();
  25. ret._bitField = 134217728;
  26. ret._settledValue = reason;
  27. this._promiseResolved(index, ret);
  28. };
  29. Promise.settle = function (promises) {
  30. return new SettledPromiseArray(promises).promise();
  31. };
  32. Promise.prototype.settle = function () {
  33. return new SettledPromiseArray(this).promise();
  34. };
  35. };