core.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. 'use strict';
  2. var asap = require('asap');
  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._h = 0;
  53. this._i = 0;
  54. this._j = null;
  55. this._k = null;
  56. if (fn === noop) return;
  57. doResolve(fn, this);
  58. }
  59. Promise._l = null;
  60. Promise._m = null;
  61. Promise._n = 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._i === 3) {
  79. self = self._j;
  80. }
  81. if (Promise._l) {
  82. Promise._l(self);
  83. }
  84. if (self._i === 0) {
  85. if (self._h === 0) {
  86. self._h = 1;
  87. self._k = deferred;
  88. return;
  89. }
  90. if (self._h === 1) {
  91. self._h = 2;
  92. self._k = [self._k, deferred];
  93. return;
  94. }
  95. self._k.push(deferred);
  96. return;
  97. }
  98. handleResolved(self, deferred);
  99. }
  100. function handleResolved(self, deferred) {
  101. asap(function() {
  102. var cb = self._i === 1 ? deferred.onFulfilled : deferred.onRejected;
  103. if (cb === null) {
  104. if (self._i === 1) {
  105. resolve(deferred.promise, self._j);
  106. } else {
  107. reject(deferred.promise, self._j);
  108. }
  109. return;
  110. }
  111. var ret = tryCallOne(cb, self._j);
  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._i = 3;
  140. self._j = newValue;
  141. finale(self);
  142. return;
  143. } else if (typeof then === 'function') {
  144. doResolve(then.bind(newValue), self);
  145. return;
  146. }
  147. }
  148. self._i = 1;
  149. self._j = newValue;
  150. finale(self);
  151. }
  152. function reject(self, newValue) {
  153. self._i = 2;
  154. self._j = newValue;
  155. if (Promise._m) {
  156. Promise._m(self, newValue);
  157. }
  158. finale(self);
  159. }
  160. function finale(self) {
  161. if (self._h === 1) {
  162. handle(self, self._k);
  163. self._k = null;
  164. }
  165. if (self._h === 2) {
  166. for (var i = 0; i < self._k.length; i++) {
  167. handle(self, self._k[i]);
  168. }
  169. self._k = 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. }