core.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. 'use strict';
  2. function noop() {}
  3. // States:
  4. //
  5. // 0 - pending
  6. // 1 - fulfilled with _value
  7. // 2 - rejected with _value
  8. // 3 - adopted the state of another promise, _value
  9. //
  10. // once the state is no longer pending (0) it is immutable
  11. // All `_` prefixed properties will be reduced to `_{random number}`
  12. // at build time to obfuscate them and discourage their use.
  13. // We don't use symbols or Object.defineProperty to fully hide them
  14. // because the performance isn't good enough.
  15. // to avoid using try/catch inside critical functions, we
  16. // extract them to here.
  17. var LAST_ERROR = null;
  18. var IS_ERROR = {};
  19. function getThen(obj) {
  20. try {
  21. return obj.then;
  22. } catch (ex) {
  23. LAST_ERROR = ex;
  24. return IS_ERROR;
  25. }
  26. }
  27. function tryCallOne(fn, a) {
  28. try {
  29. return fn(a);
  30. } catch (ex) {
  31. LAST_ERROR = ex;
  32. return IS_ERROR;
  33. }
  34. }
  35. function tryCallTwo(fn, a, b) {
  36. try {
  37. fn(a, b);
  38. } catch (ex) {
  39. LAST_ERROR = ex;
  40. return IS_ERROR;
  41. }
  42. }
  43. module.exports = Promise;
  44. function Promise(fn) {
  45. if (typeof this !== 'object') {
  46. throw new TypeError('Promises must be constructed via new');
  47. }
  48. if (typeof fn !== 'function') {
  49. throw new TypeError('Promise constructor\'s argument is not a function');
  50. }
  51. this._40 = 0;
  52. this._65 = 0;
  53. this._55 = null;
  54. this._72 = null;
  55. if (fn === noop) return;
  56. doResolve(fn, this);
  57. }
  58. Promise._37 = null;
  59. Promise._87 = null;
  60. Promise._61 = noop;
  61. Promise.prototype.then = function(onFulfilled, onRejected) {
  62. if (this.constructor !== Promise) {
  63. return safeThen(this, onFulfilled, onRejected);
  64. }
  65. var res = new Promise(noop);
  66. handle(this, new Handler(onFulfilled, onRejected, res));
  67. return res;
  68. };
  69. function safeThen(self, onFulfilled, onRejected) {
  70. return new self.constructor(function (resolve, reject) {
  71. var res = new Promise(noop);
  72. res.then(resolve, reject);
  73. handle(self, new Handler(onFulfilled, onRejected, res));
  74. });
  75. }
  76. function handle(self, deferred) {
  77. while (self._65 === 3) {
  78. self = self._55;
  79. }
  80. if (Promise._37) {
  81. Promise._37(self);
  82. }
  83. if (self._65 === 0) {
  84. if (self._40 === 0) {
  85. self._40 = 1;
  86. self._72 = deferred;
  87. return;
  88. }
  89. if (self._40 === 1) {
  90. self._40 = 2;
  91. self._72 = [self._72, deferred];
  92. return;
  93. }
  94. self._72.push(deferred);
  95. return;
  96. }
  97. handleResolved(self, deferred);
  98. }
  99. function handleResolved(self, deferred) {
  100. setImmediate(function() {
  101. var cb = self._65 === 1 ? deferred.onFulfilled : deferred.onRejected;
  102. if (cb === null) {
  103. if (self._65 === 1) {
  104. resolve(deferred.promise, self._55);
  105. } else {
  106. reject(deferred.promise, self._55);
  107. }
  108. return;
  109. }
  110. var ret = tryCallOne(cb, self._55);
  111. if (ret === IS_ERROR) {
  112. reject(deferred.promise, LAST_ERROR);
  113. } else {
  114. resolve(deferred.promise, ret);
  115. }
  116. });
  117. }
  118. function resolve(self, newValue) {
  119. // Promise Resolution Procedure: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure
  120. if (newValue === self) {
  121. return reject(
  122. self,
  123. new TypeError('A promise cannot be resolved with itself.')
  124. );
  125. }
  126. if (
  127. newValue &&
  128. (typeof newValue === 'object' || typeof newValue === 'function')
  129. ) {
  130. var then = getThen(newValue);
  131. if (then === IS_ERROR) {
  132. return reject(self, LAST_ERROR);
  133. }
  134. if (
  135. then === self.then &&
  136. newValue instanceof Promise
  137. ) {
  138. self._65 = 3;
  139. self._55 = newValue;
  140. finale(self);
  141. return;
  142. } else if (typeof then === 'function') {
  143. doResolve(then.bind(newValue), self);
  144. return;
  145. }
  146. }
  147. self._65 = 1;
  148. self._55 = newValue;
  149. finale(self);
  150. }
  151. function reject(self, newValue) {
  152. self._65 = 2;
  153. self._55 = newValue;
  154. if (Promise._87) {
  155. Promise._87(self, newValue);
  156. }
  157. finale(self);
  158. }
  159. function finale(self) {
  160. if (self._40 === 1) {
  161. handle(self, self._72);
  162. self._72 = null;
  163. }
  164. if (self._40 === 2) {
  165. for (var i = 0; i < self._72.length; i++) {
  166. handle(self, self._72[i]);
  167. }
  168. self._72 = null;
  169. }
  170. }
  171. function Handler(onFulfilled, onRejected, promise){
  172. this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null;
  173. this.onRejected = typeof onRejected === 'function' ? onRejected : null;
  174. this.promise = promise;
  175. }
  176. /**
  177. * Take a potentially misbehaving resolver function and make sure
  178. * onFulfilled and onRejected are only called once.
  179. *
  180. * Makes no guarantees about asynchrony.
  181. */
  182. function doResolve(fn, promise) {
  183. var done = false;
  184. var res = tryCallTwo(fn, function (value) {
  185. if (done) return;
  186. done = true;
  187. resolve(promise, value);
  188. }, function (reason) {
  189. if (done) return;
  190. done = true;
  191. reject(promise, reason);
  192. });
  193. if (!done && res === IS_ERROR) {
  194. done = true;
  195. reject(promise, LAST_ERROR);
  196. }
  197. }