You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.0 KiB

  1. 'use strict';
  2. //This file contains the ES6 extensions to the core Promises/A+ API
  3. var Promise = require('./core.js');
  4. module.exports = Promise;
  5. /* Static Functions */
  6. var TRUE = valuePromise(true);
  7. var FALSE = valuePromise(false);
  8. var NULL = valuePromise(null);
  9. var UNDEFINED = valuePromise(undefined);
  10. var ZERO = valuePromise(0);
  11. var EMPTYSTRING = valuePromise('');
  12. function valuePromise(value) {
  13. var p = new Promise(Promise._0);
  14. p._V = 1;
  15. p._W = value;
  16. return p;
  17. }
  18. Promise.resolve = function (value) {
  19. if (value instanceof Promise) return value;
  20. if (value === null) return NULL;
  21. if (value === undefined) return UNDEFINED;
  22. if (value === true) return TRUE;
  23. if (value === false) return FALSE;
  24. if (value === 0) return ZERO;
  25. if (value === '') return EMPTYSTRING;
  26. if (typeof value === 'object' || typeof value === 'function') {
  27. try {
  28. var then = value.then;
  29. if (typeof then === 'function') {
  30. return new Promise(then.bind(value));
  31. }
  32. } catch (ex) {
  33. return new Promise(function (resolve, reject) {
  34. reject(ex);
  35. });
  36. }
  37. }
  38. return valuePromise(value);
  39. };
  40. var iterableToArray = function (iterable) {
  41. if (typeof Array.from === 'function') {
  42. // ES2015+, iterables exist
  43. iterableToArray = Array.from;
  44. return Array.from(iterable);
  45. }
  46. // ES5, only arrays and array-likes exist
  47. iterableToArray = function (x) { return Array.prototype.slice.call(x); };
  48. return Array.prototype.slice.call(iterable);
  49. }
  50. Promise.all = function (arr) {
  51. var args = iterableToArray(arr);
  52. return new Promise(function (resolve, reject) {
  53. if (args.length === 0) return resolve([]);
  54. var remaining = args.length;
  55. function res(i, val) {
  56. if (val && (typeof val === 'object' || typeof val === 'function')) {
  57. if (val instanceof Promise && val.then === Promise.prototype.then) {
  58. while (val._V === 3) {
  59. val = val._W;
  60. }
  61. if (val._V === 1) return res(i, val._W);
  62. if (val._V === 2) reject(val._W);
  63. val.then(function (val) {
  64. res(i, val);
  65. }, reject);
  66. return;
  67. } else {
  68. var then = val.then;
  69. if (typeof then === 'function') {
  70. var p = new Promise(then.bind(val));
  71. p.then(function (val) {
  72. res(i, val);
  73. }, reject);
  74. return;
  75. }
  76. }
  77. }
  78. args[i] = val;
  79. if (--remaining === 0) {
  80. resolve(args);
  81. }
  82. }
  83. for (var i = 0; i < args.length; i++) {
  84. res(i, args[i]);
  85. }
  86. });
  87. };
  88. Promise.reject = function (value) {
  89. return new Promise(function (resolve, reject) {
  90. reject(value);
  91. });
  92. };
  93. Promise.race = function (values) {
  94. return new Promise(function (resolve, reject) {
  95. iterableToArray(values).forEach(function(value){
  96. Promise.resolve(value).then(resolve, reject);
  97. });
  98. });
  99. };
  100. /* Prototype Methods */
  101. Promise.prototype['catch'] = function (onRejected) {
  102. return this.then(null, onRejected);
  103. };