es6-extensions.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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._61);
  14. p._65 = 1;
  15. p._55 = 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. Promise.all = function (arr) {
  41. var args = Array.prototype.slice.call(arr);
  42. return new Promise(function (resolve, reject) {
  43. if (args.length === 0) return resolve([]);
  44. var remaining = args.length;
  45. function res(i, val) {
  46. if (val && (typeof val === 'object' || typeof val === 'function')) {
  47. if (val instanceof Promise && val.then === Promise.prototype.then) {
  48. while (val._65 === 3) {
  49. val = val._55;
  50. }
  51. if (val._65 === 1) return res(i, val._55);
  52. if (val._65 === 2) reject(val._55);
  53. val.then(function (val) {
  54. res(i, val);
  55. }, reject);
  56. return;
  57. } else {
  58. var then = val.then;
  59. if (typeof then === 'function') {
  60. var p = new Promise(then.bind(val));
  61. p.then(function (val) {
  62. res(i, val);
  63. }, reject);
  64. return;
  65. }
  66. }
  67. }
  68. args[i] = val;
  69. if (--remaining === 0) {
  70. resolve(args);
  71. }
  72. }
  73. for (var i = 0; i < args.length; i++) {
  74. res(i, args[i]);
  75. }
  76. });
  77. };
  78. Promise.reject = function (value) {
  79. return new Promise(function (resolve, reject) {
  80. reject(value);
  81. });
  82. };
  83. Promise.race = function (values) {
  84. return new Promise(function (resolve, reject) {
  85. values.forEach(function(value){
  86. Promise.resolve(value).then(resolve, reject);
  87. });
  88. });
  89. };
  90. /* Prototype Methods */
  91. Promise.prototype['catch'] = function (onRejected) {
  92. return this.then(null, onRejected);
  93. };