promise.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. "use strict";
  2. module.exports = function() {
  3. var makeSelfResolutionError = function () {
  4. return new TypeError("circular promise resolution chain\u000a\u000a See http://goo.gl/LhFpo0\u000a");
  5. };
  6. var reflect = function() {
  7. return new Promise.PromiseInspection(this._target());
  8. };
  9. var apiRejection = function(msg) {
  10. return Promise.reject(new TypeError(msg));
  11. };
  12. var util = require("./util.js");
  13. var async = require("./async.js");
  14. var errors = require("./errors.js");
  15. var TypeError = Promise.TypeError = errors.TypeError;
  16. Promise.RangeError = errors.RangeError;
  17. Promise.CancellationError = errors.CancellationError;
  18. Promise.TimeoutError = errors.TimeoutError;
  19. Promise.OperationalError = errors.OperationalError;
  20. Promise.RejectionError = errors.OperationalError;
  21. Promise.AggregateError = errors.AggregateError;
  22. var INTERNAL = function(){};
  23. var APPLY = {};
  24. var NEXT_FILTER = {e: null};
  25. var tryConvertToPromise = require("./thenables.js")(Promise, INTERNAL);
  26. var PromiseArray =
  27. require("./promise_array.js")(Promise, INTERNAL,
  28. tryConvertToPromise, apiRejection);
  29. var CapturedTrace = require("./captured_trace.js")();
  30. var isDebugging = require("./debuggability.js")(Promise, CapturedTrace);
  31. /*jshint unused:false*/
  32. var createContext =
  33. require("./context.js")(Promise, CapturedTrace, isDebugging);
  34. var CatchFilter = require("./catch_filter.js")(NEXT_FILTER);
  35. var PromiseResolver = require("./promise_resolver.js");
  36. var nodebackForPromise = PromiseResolver._nodebackForPromise;
  37. var errorObj = util.errorObj;
  38. var tryCatch = util.tryCatch;
  39. function Promise(resolver) {
  40. if (typeof resolver !== "function") {
  41. throw new TypeError("the promise constructor requires a resolver function\u000a\u000a See http://goo.gl/EC22Yn\u000a");
  42. }
  43. if (this.constructor !== Promise) {
  44. throw new TypeError("the promise constructor cannot be invoked directly\u000a\u000a See http://goo.gl/KsIlge\u000a");
  45. }
  46. this._bitField = 0;
  47. this._fulfillmentHandler0 = undefined;
  48. this._rejectionHandler0 = undefined;
  49. this._progressHandler0 = undefined;
  50. this._promise0 = undefined;
  51. this._receiver0 = undefined;
  52. this._settledValue = undefined;
  53. if (resolver !== INTERNAL) this._resolveFromResolver(resolver);
  54. }
  55. Promise.prototype.toString = function () {
  56. return "[object Promise]";
  57. };
  58. Promise.prototype.caught = Promise.prototype["catch"] = function (fn) {
  59. var len = arguments.length;
  60. if (len > 1) {
  61. var catchInstances = new Array(len - 1),
  62. j = 0, i;
  63. for (i = 0; i < len - 1; ++i) {
  64. var item = arguments[i];
  65. if (typeof item === "function") {
  66. catchInstances[j++] = item;
  67. } else {
  68. return Promise.reject(
  69. new TypeError("Catch filter must inherit from Error or be a simple predicate function\u000a\u000a See http://goo.gl/o84o68\u000a"));
  70. }
  71. }
  72. catchInstances.length = j;
  73. fn = arguments[i];
  74. var catchFilter = new CatchFilter(catchInstances, fn, this);
  75. return this._then(undefined, catchFilter.doFilter, undefined,
  76. catchFilter, undefined);
  77. }
  78. return this._then(undefined, fn, undefined, undefined, undefined);
  79. };
  80. Promise.prototype.reflect = function () {
  81. return this._then(reflect, reflect, undefined, this, undefined);
  82. };
  83. Promise.prototype.then = function (didFulfill, didReject, didProgress) {
  84. if (isDebugging() && arguments.length > 0 &&
  85. typeof didFulfill !== "function" &&
  86. typeof didReject !== "function") {
  87. var msg = ".then() only accepts functions but was passed: " +
  88. util.classString(didFulfill);
  89. if (arguments.length > 1) {
  90. msg += ", " + util.classString(didReject);
  91. }
  92. this._warn(msg);
  93. }
  94. return this._then(didFulfill, didReject, didProgress,
  95. undefined, undefined);
  96. };
  97. Promise.prototype.done = function (didFulfill, didReject, didProgress) {
  98. var promise = this._then(didFulfill, didReject, didProgress,
  99. undefined, undefined);
  100. promise._setIsFinal();
  101. };
  102. Promise.prototype.spread = function (didFulfill, didReject) {
  103. return this.all()._then(didFulfill, didReject, undefined, APPLY, undefined);
  104. };
  105. Promise.prototype.isCancellable = function () {
  106. return !this.isResolved() &&
  107. this._cancellable();
  108. };
  109. Promise.prototype.toJSON = function () {
  110. var ret = {
  111. isFulfilled: false,
  112. isRejected: false,
  113. fulfillmentValue: undefined,
  114. rejectionReason: undefined
  115. };
  116. if (this.isFulfilled()) {
  117. ret.fulfillmentValue = this.value();
  118. ret.isFulfilled = true;
  119. } else if (this.isRejected()) {
  120. ret.rejectionReason = this.reason();
  121. ret.isRejected = true;
  122. }
  123. return ret;
  124. };
  125. Promise.prototype.all = function () {
  126. return new PromiseArray(this).promise();
  127. };
  128. Promise.prototype.error = function (fn) {
  129. return this.caught(util.originatesFromRejection, fn);
  130. };
  131. Promise.is = function (val) {
  132. return val instanceof Promise;
  133. };
  134. Promise.fromNode = function(fn) {
  135. var ret = new Promise(INTERNAL);
  136. var result = tryCatch(fn)(nodebackForPromise(ret));
  137. if (result === errorObj) {
  138. ret._rejectCallback(result.e, true, true);
  139. }
  140. return ret;
  141. };
  142. Promise.all = function (promises) {
  143. return new PromiseArray(promises).promise();
  144. };
  145. Promise.defer = Promise.pending = function () {
  146. var promise = new Promise(INTERNAL);
  147. return new PromiseResolver(promise);
  148. };
  149. Promise.cast = function (obj) {
  150. var ret = tryConvertToPromise(obj);
  151. if (!(ret instanceof Promise)) {
  152. var val = ret;
  153. ret = new Promise(INTERNAL);
  154. ret._fulfillUnchecked(val);
  155. }
  156. return ret;
  157. };
  158. Promise.resolve = Promise.fulfilled = Promise.cast;
  159. Promise.reject = Promise.rejected = function (reason) {
  160. var ret = new Promise(INTERNAL);
  161. ret._captureStackTrace();
  162. ret._rejectCallback(reason, true);
  163. return ret;
  164. };
  165. Promise.setScheduler = function(fn) {
  166. if (typeof fn !== "function") throw new TypeError("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
  167. var prev = async._schedule;
  168. async._schedule = fn;
  169. return prev;
  170. };
  171. Promise.prototype._then = function (
  172. didFulfill,
  173. didReject,
  174. didProgress,
  175. receiver,
  176. internalData
  177. ) {
  178. var haveInternalData = internalData !== undefined;
  179. var ret = haveInternalData ? internalData : new Promise(INTERNAL);
  180. if (!haveInternalData) {
  181. ret._propagateFrom(this, 4 | 1);
  182. ret._captureStackTrace();
  183. }
  184. var target = this._target();
  185. if (target !== this) {
  186. if (receiver === undefined) receiver = this._boundTo;
  187. if (!haveInternalData) ret._setIsMigrated();
  188. }
  189. var callbackIndex =
  190. target._addCallbacks(didFulfill, didReject, didProgress, ret, receiver);
  191. if (target._isResolved() && !target._isSettlePromisesQueued()) {
  192. async.invoke(
  193. target._settlePromiseAtPostResolution, target, callbackIndex);
  194. }
  195. return ret;
  196. };
  197. Promise.prototype._settlePromiseAtPostResolution = function (index) {
  198. if (this._isRejectionUnhandled()) this._unsetRejectionIsUnhandled();
  199. this._settlePromiseAt(index);
  200. };
  201. Promise.prototype._length = function () {
  202. return this._bitField & 131071;
  203. };
  204. Promise.prototype._isFollowingOrFulfilledOrRejected = function () {
  205. return (this._bitField & 939524096) > 0;
  206. };
  207. Promise.prototype._isFollowing = function () {
  208. return (this._bitField & 536870912) === 536870912;
  209. };
  210. Promise.prototype._setLength = function (len) {
  211. this._bitField = (this._bitField & -131072) |
  212. (len & 131071);
  213. };
  214. Promise.prototype._setFulfilled = function () {
  215. this._bitField = this._bitField | 268435456;
  216. };
  217. Promise.prototype._setRejected = function () {
  218. this._bitField = this._bitField | 134217728;
  219. };
  220. Promise.prototype._setFollowing = function () {
  221. this._bitField = this._bitField | 536870912;
  222. };
  223. Promise.prototype._setIsFinal = function () {
  224. this._bitField = this._bitField | 33554432;
  225. };
  226. Promise.prototype._isFinal = function () {
  227. return (this._bitField & 33554432) > 0;
  228. };
  229. Promise.prototype._cancellable = function () {
  230. return (this._bitField & 67108864) > 0;
  231. };
  232. Promise.prototype._setCancellable = function () {
  233. this._bitField = this._bitField | 67108864;
  234. };
  235. Promise.prototype._unsetCancellable = function () {
  236. this._bitField = this._bitField & (~67108864);
  237. };
  238. Promise.prototype._setIsMigrated = function () {
  239. this._bitField = this._bitField | 4194304;
  240. };
  241. Promise.prototype._unsetIsMigrated = function () {
  242. this._bitField = this._bitField & (~4194304);
  243. };
  244. Promise.prototype._isMigrated = function () {
  245. return (this._bitField & 4194304) > 0;
  246. };
  247. Promise.prototype._receiverAt = function (index) {
  248. var ret = index === 0
  249. ? this._receiver0
  250. : this[
  251. index * 5 - 5 + 4];
  252. if (ret === undefined && this._isBound()) {
  253. return this._boundTo;
  254. }
  255. return ret;
  256. };
  257. Promise.prototype._promiseAt = function (index) {
  258. return index === 0
  259. ? this._promise0
  260. : this[index * 5 - 5 + 3];
  261. };
  262. Promise.prototype._fulfillmentHandlerAt = function (index) {
  263. return index === 0
  264. ? this._fulfillmentHandler0
  265. : this[index * 5 - 5 + 0];
  266. };
  267. Promise.prototype._rejectionHandlerAt = function (index) {
  268. return index === 0
  269. ? this._rejectionHandler0
  270. : this[index * 5 - 5 + 1];
  271. };
  272. Promise.prototype._migrateCallbacks = function (follower, index) {
  273. var fulfill = follower._fulfillmentHandlerAt(index);
  274. var reject = follower._rejectionHandlerAt(index);
  275. var progress = follower._progressHandlerAt(index);
  276. var promise = follower._promiseAt(index);
  277. var receiver = follower._receiverAt(index);
  278. if (promise instanceof Promise) promise._setIsMigrated();
  279. this._addCallbacks(fulfill, reject, progress, promise, receiver);
  280. };
  281. Promise.prototype._addCallbacks = function (
  282. fulfill,
  283. reject,
  284. progress,
  285. promise,
  286. receiver
  287. ) {
  288. var index = this._length();
  289. if (index >= 131071 - 5) {
  290. index = 0;
  291. this._setLength(0);
  292. }
  293. if (index === 0) {
  294. this._promise0 = promise;
  295. if (receiver !== undefined) this._receiver0 = receiver;
  296. if (typeof fulfill === "function" && !this._isCarryingStackTrace())
  297. this._fulfillmentHandler0 = fulfill;
  298. if (typeof reject === "function") this._rejectionHandler0 = reject;
  299. if (typeof progress === "function") this._progressHandler0 = progress;
  300. } else {
  301. var base = index * 5 - 5;
  302. this[base + 3] = promise;
  303. this[base + 4] = receiver;
  304. if (typeof fulfill === "function")
  305. this[base + 0] = fulfill;
  306. if (typeof reject === "function")
  307. this[base + 1] = reject;
  308. if (typeof progress === "function")
  309. this[base + 2] = progress;
  310. }
  311. this._setLength(index + 1);
  312. return index;
  313. };
  314. Promise.prototype._setProxyHandlers = function (receiver, promiseSlotValue) {
  315. var index = this._length();
  316. if (index >= 131071 - 5) {
  317. index = 0;
  318. this._setLength(0);
  319. }
  320. if (index === 0) {
  321. this._promise0 = promiseSlotValue;
  322. this._receiver0 = receiver;
  323. } else {
  324. var base = index * 5 - 5;
  325. this[base + 3] = promiseSlotValue;
  326. this[base + 4] = receiver;
  327. }
  328. this._setLength(index + 1);
  329. };
  330. Promise.prototype._proxyPromiseArray = function (promiseArray, index) {
  331. this._setProxyHandlers(promiseArray, index);
  332. };
  333. Promise.prototype._resolveCallback = function(value, shouldBind) {
  334. if (this._isFollowingOrFulfilledOrRejected()) return;
  335. if (value === this)
  336. return this._rejectCallback(makeSelfResolutionError(), false, true);
  337. var maybePromise = tryConvertToPromise(value, this);
  338. if (!(maybePromise instanceof Promise)) return this._fulfill(value);
  339. var propagationFlags = 1 | (shouldBind ? 4 : 0);
  340. this._propagateFrom(maybePromise, propagationFlags);
  341. var promise = maybePromise._target();
  342. if (promise._isPending()) {
  343. var len = this._length();
  344. for (var i = 0; i < len; ++i) {
  345. promise._migrateCallbacks(this, i);
  346. }
  347. this._setFollowing();
  348. this._setLength(0);
  349. this._setFollowee(promise);
  350. } else if (promise._isFulfilled()) {
  351. this._fulfillUnchecked(promise._value());
  352. } else {
  353. this._rejectUnchecked(promise._reason(),
  354. promise._getCarriedStackTrace());
  355. }
  356. };
  357. Promise.prototype._rejectCallback =
  358. function(reason, synchronous, shouldNotMarkOriginatingFromRejection) {
  359. if (!shouldNotMarkOriginatingFromRejection) {
  360. util.markAsOriginatingFromRejection(reason);
  361. }
  362. var trace = util.ensureErrorObject(reason);
  363. var hasStack = trace === reason;
  364. this._attachExtraTrace(trace, synchronous ? hasStack : false);
  365. this._reject(reason, hasStack ? undefined : trace);
  366. };
  367. Promise.prototype._resolveFromResolver = function (resolver) {
  368. var promise = this;
  369. this._captureStackTrace();
  370. this._pushContext();
  371. var synchronous = true;
  372. var r = tryCatch(resolver)(function(value) {
  373. if (promise === null) return;
  374. promise._resolveCallback(value);
  375. promise = null;
  376. }, function (reason) {
  377. if (promise === null) return;
  378. promise._rejectCallback(reason, synchronous);
  379. promise = null;
  380. });
  381. synchronous = false;
  382. this._popContext();
  383. if (r !== undefined && r === errorObj && promise !== null) {
  384. promise._rejectCallback(r.e, true, true);
  385. promise = null;
  386. }
  387. };
  388. Promise.prototype._settlePromiseFromHandler = function (
  389. handler, receiver, value, promise
  390. ) {
  391. if (promise._isRejected()) return;
  392. promise._pushContext();
  393. var x;
  394. if (receiver === APPLY && !this._isRejected()) {
  395. x = tryCatch(handler).apply(this._boundTo, value);
  396. } else {
  397. x = tryCatch(handler).call(receiver, value);
  398. }
  399. promise._popContext();
  400. if (x === errorObj || x === promise || x === NEXT_FILTER) {
  401. var err = x === promise ? makeSelfResolutionError() : x.e;
  402. promise._rejectCallback(err, false, true);
  403. } else {
  404. promise._resolveCallback(x);
  405. }
  406. };
  407. Promise.prototype._target = function() {
  408. var ret = this;
  409. while (ret._isFollowing()) ret = ret._followee();
  410. return ret;
  411. };
  412. Promise.prototype._followee = function() {
  413. return this._rejectionHandler0;
  414. };
  415. Promise.prototype._setFollowee = function(promise) {
  416. this._rejectionHandler0 = promise;
  417. };
  418. Promise.prototype._cleanValues = function () {
  419. if (this._cancellable()) {
  420. this._cancellationParent = undefined;
  421. }
  422. };
  423. Promise.prototype._propagateFrom = function (parent, flags) {
  424. if ((flags & 1) > 0 && parent._cancellable()) {
  425. this._setCancellable();
  426. this._cancellationParent = parent;
  427. }
  428. if ((flags & 4) > 0 && parent._isBound()) {
  429. this._setBoundTo(parent._boundTo);
  430. }
  431. };
  432. Promise.prototype._fulfill = function (value) {
  433. if (this._isFollowingOrFulfilledOrRejected()) return;
  434. this._fulfillUnchecked(value);
  435. };
  436. Promise.prototype._reject = function (reason, carriedStackTrace) {
  437. if (this._isFollowingOrFulfilledOrRejected()) return;
  438. this._rejectUnchecked(reason, carriedStackTrace);
  439. };
  440. Promise.prototype._settlePromiseAt = function (index) {
  441. var promise = this._promiseAt(index);
  442. var isPromise = promise instanceof Promise;
  443. if (isPromise && promise._isMigrated()) {
  444. promise._unsetIsMigrated();
  445. return async.invoke(this._settlePromiseAt, this, index);
  446. }
  447. var handler = this._isFulfilled()
  448. ? this._fulfillmentHandlerAt(index)
  449. : this._rejectionHandlerAt(index);
  450. var carriedStackTrace =
  451. this._isCarryingStackTrace() ? this._getCarriedStackTrace() : undefined;
  452. var value = this._settledValue;
  453. var receiver = this._receiverAt(index);
  454. this._clearCallbackDataAtIndex(index);
  455. if (typeof handler === "function") {
  456. if (!isPromise) {
  457. handler.call(receiver, value, promise);
  458. } else {
  459. this._settlePromiseFromHandler(handler, receiver, value, promise);
  460. }
  461. } else if (receiver instanceof PromiseArray) {
  462. if (!receiver._isResolved()) {
  463. if (this._isFulfilled()) {
  464. receiver._promiseFulfilled(value, promise);
  465. }
  466. else {
  467. receiver._promiseRejected(value, promise);
  468. }
  469. }
  470. } else if (isPromise) {
  471. if (this._isFulfilled()) {
  472. promise._fulfill(value);
  473. } else {
  474. promise._reject(value, carriedStackTrace);
  475. }
  476. }
  477. if (index >= 4 && (index & 31) === 4)
  478. async.invokeLater(this._setLength, this, 0);
  479. };
  480. Promise.prototype._clearCallbackDataAtIndex = function(index) {
  481. if (index === 0) {
  482. if (!this._isCarryingStackTrace()) {
  483. this._fulfillmentHandler0 = undefined;
  484. }
  485. this._rejectionHandler0 =
  486. this._progressHandler0 =
  487. this._receiver0 =
  488. this._promise0 = undefined;
  489. } else {
  490. var base = index * 5 - 5;
  491. this[base + 3] =
  492. this[base + 4] =
  493. this[base + 0] =
  494. this[base + 1] =
  495. this[base + 2] = undefined;
  496. }
  497. };
  498. Promise.prototype._isSettlePromisesQueued = function () {
  499. return (this._bitField &
  500. -1073741824) === -1073741824;
  501. };
  502. Promise.prototype._setSettlePromisesQueued = function () {
  503. this._bitField = this._bitField | -1073741824;
  504. };
  505. Promise.prototype._unsetSettlePromisesQueued = function () {
  506. this._bitField = this._bitField & (~-1073741824);
  507. };
  508. Promise.prototype._queueSettlePromises = function() {
  509. async.settlePromises(this);
  510. this._setSettlePromisesQueued();
  511. };
  512. Promise.prototype._fulfillUnchecked = function (value) {
  513. if (value === this) {
  514. var err = makeSelfResolutionError();
  515. this._attachExtraTrace(err);
  516. return this._rejectUnchecked(err, undefined);
  517. }
  518. this._setFulfilled();
  519. this._settledValue = value;
  520. this._cleanValues();
  521. if (this._length() > 0) {
  522. this._queueSettlePromises();
  523. }
  524. };
  525. Promise.prototype._rejectUncheckedCheckError = function (reason) {
  526. var trace = util.ensureErrorObject(reason);
  527. this._rejectUnchecked(reason, trace === reason ? undefined : trace);
  528. };
  529. Promise.prototype._rejectUnchecked = function (reason, trace) {
  530. if (reason === this) {
  531. var err = makeSelfResolutionError();
  532. this._attachExtraTrace(err);
  533. return this._rejectUnchecked(err);
  534. }
  535. this._setRejected();
  536. this._settledValue = reason;
  537. this._cleanValues();
  538. if (this._isFinal()) {
  539. async.throwLater(function(e) {
  540. if ("stack" in e) {
  541. async.invokeFirst(
  542. CapturedTrace.unhandledRejection, undefined, e);
  543. }
  544. throw e;
  545. }, trace === undefined ? reason : trace);
  546. return;
  547. }
  548. if (trace !== undefined && trace !== reason) {
  549. this._setCarriedStackTrace(trace);
  550. }
  551. if (this._length() > 0) {
  552. this._queueSettlePromises();
  553. } else {
  554. this._ensurePossibleRejectionHandled();
  555. }
  556. };
  557. Promise.prototype._settlePromises = function () {
  558. this._unsetSettlePromisesQueued();
  559. var len = this._length();
  560. for (var i = 0; i < len; i++) {
  561. this._settlePromiseAt(i);
  562. }
  563. };
  564. Promise._makeSelfResolutionError = makeSelfResolutionError;
  565. require("./progress.js")(Promise, PromiseArray);
  566. require("./method.js")(Promise, INTERNAL, tryConvertToPromise, apiRejection);
  567. require("./bind.js")(Promise, INTERNAL, tryConvertToPromise);
  568. require("./finally.js")(Promise, NEXT_FILTER, tryConvertToPromise);
  569. require("./direct_resolve.js")(Promise);
  570. require("./synchronous_inspection.js")(Promise);
  571. require("./join.js")(Promise, PromiseArray, tryConvertToPromise, INTERNAL);
  572. Promise.Promise = Promise;
  573. require('./map.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL);
  574. require('./cancel.js')(Promise);
  575. require('./using.js')(Promise, apiRejection, tryConvertToPromise, createContext);
  576. require('./generators.js')(Promise, apiRejection, INTERNAL, tryConvertToPromise);
  577. require('./nodeify.js')(Promise);
  578. require('./call_get.js')(Promise);
  579. require('./props.js')(Promise, PromiseArray, tryConvertToPromise, apiRejection);
  580. require('./race.js')(Promise, INTERNAL, tryConvertToPromise, apiRejection);
  581. require('./reduce.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL);
  582. require('./settle.js')(Promise, PromiseArray);
  583. require('./some.js')(Promise, PromiseArray, apiRejection);
  584. require('./promisify.js')(Promise, INTERNAL);
  585. require('./any.js')(Promise);
  586. require('./each.js')(Promise, INTERNAL);
  587. require('./timers.js')(Promise, INTERNAL);
  588. require('./filter.js')(Promise, INTERNAL);
  589. util.toFastProperties(Promise);
  590. util.toFastProperties(Promise.prototype);
  591. function fillTypes(value) {
  592. var p = new Promise(INTERNAL);
  593. p._fulfillmentHandler0 = value;
  594. p._rejectionHandler0 = value;
  595. p._progressHandler0 = value;
  596. p._promise0 = value;
  597. p._receiver0 = value;
  598. p._settledValue = value;
  599. }
  600. // Complete slack tracking, opt out of field-type tracking and
  601. // stabilize map
  602. fillTypes({a: 1});
  603. fillTypes({b: 2});
  604. fillTypes({c: 3});
  605. fillTypes(1);
  606. fillTypes(function(){});
  607. fillTypes(undefined);
  608. fillTypes(false);
  609. fillTypes(new Promise(INTERNAL));
  610. CapturedTrace.setBounds(async.firstLineError, util.lastLineError);
  611. return Promise;
  612. };