core.js 4.7 KB

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