You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

819 lines
26 KiB

4 years ago
  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/MqrFmX\u000a");
  5. };
  6. var reflectHandler = function() {
  7. return new Promise.PromiseInspection(this._target());
  8. };
  9. var apiRejection = function(msg) {
  10. return Promise.reject(new TypeError(msg));
  11. };
  12. function Proxyable() {}
  13. var UNDEFINED_BINDING = {};
  14. var util = require("./util");
  15. util.setReflectHandler(reflectHandler);
  16. var getDomain = function() {
  17. var domain = process.domain;
  18. if (domain === undefined) {
  19. return null;
  20. }
  21. return domain;
  22. };
  23. var getContextDefault = function() {
  24. return null;
  25. };
  26. var getContextDomain = function() {
  27. return {
  28. domain: getDomain(),
  29. async: null
  30. };
  31. };
  32. var AsyncResource = util.isNode && util.nodeSupportsAsyncResource ?
  33. require("async_hooks").AsyncResource : null;
  34. var getContextAsyncHooks = function() {
  35. return {
  36. domain: getDomain(),
  37. async: new AsyncResource("Bluebird::Promise")
  38. };
  39. };
  40. var getContext = util.isNode ? getContextDomain : getContextDefault;
  41. util.notEnumerableProp(Promise, "_getContext", getContext);
  42. var enableAsyncHooks = function() {
  43. getContext = getContextAsyncHooks;
  44. util.notEnumerableProp(Promise, "_getContext", getContextAsyncHooks);
  45. };
  46. var disableAsyncHooks = function() {
  47. getContext = getContextDomain;
  48. util.notEnumerableProp(Promise, "_getContext", getContextDomain);
  49. };
  50. var es5 = require("./es5");
  51. var Async = require("./async");
  52. var async = new Async();
  53. es5.defineProperty(Promise, "_async", {value: async});
  54. var errors = require("./errors");
  55. var TypeError = Promise.TypeError = errors.TypeError;
  56. Promise.RangeError = errors.RangeError;
  57. var CancellationError = Promise.CancellationError = errors.CancellationError;
  58. Promise.TimeoutError = errors.TimeoutError;
  59. Promise.OperationalError = errors.OperationalError;
  60. Promise.RejectionError = errors.OperationalError;
  61. Promise.AggregateError = errors.AggregateError;
  62. var INTERNAL = function(){};
  63. var APPLY = {};
  64. var NEXT_FILTER = {};
  65. var tryConvertToPromise = require("./thenables")(Promise, INTERNAL);
  66. var PromiseArray =
  67. require("./promise_array")(Promise, INTERNAL,
  68. tryConvertToPromise, apiRejection, Proxyable);
  69. var Context = require("./context")(Promise);
  70. /*jshint unused:false*/
  71. var createContext = Context.create;
  72. var debug = require("./debuggability")(Promise, Context,
  73. enableAsyncHooks, disableAsyncHooks);
  74. var CapturedTrace = debug.CapturedTrace;
  75. var PassThroughHandlerContext =
  76. require("./finally")(Promise, tryConvertToPromise, NEXT_FILTER);
  77. var catchFilter = require("./catch_filter")(NEXT_FILTER);
  78. var nodebackForPromise = require("./nodeback");
  79. var errorObj = util.errorObj;
  80. var tryCatch = util.tryCatch;
  81. function check(self, executor) {
  82. if (self == null || self.constructor !== Promise) {
  83. throw new TypeError("the promise constructor cannot be invoked directly\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  84. }
  85. if (typeof executor !== "function") {
  86. throw new TypeError("expecting a function but got " + util.classString(executor));
  87. }
  88. }
  89. function Promise(executor) {
  90. if (executor !== INTERNAL) {
  91. check(this, executor);
  92. }
  93. this._bitField = 0;
  94. this._fulfillmentHandler0 = undefined;
  95. this._rejectionHandler0 = undefined;
  96. this._promise0 = undefined;
  97. this._receiver0 = undefined;
  98. this._resolveFromExecutor(executor);
  99. this._promiseCreated();
  100. this._fireEvent("promiseCreated", this);
  101. }
  102. Promise.prototype.toString = function () {
  103. return "[object Promise]";
  104. };
  105. Promise.prototype.caught = Promise.prototype["catch"] = function (fn) {
  106. var len = arguments.length;
  107. if (len > 1) {
  108. var catchInstances = new Array(len - 1),
  109. j = 0, i;
  110. for (i = 0; i < len - 1; ++i) {
  111. var item = arguments[i];
  112. if (util.isObject(item)) {
  113. catchInstances[j++] = item;
  114. } else {
  115. return apiRejection("Catch statement predicate: " +
  116. "expecting an object but got " + util.classString(item));
  117. }
  118. }
  119. catchInstances.length = j;
  120. fn = arguments[i];
  121. if (typeof fn !== "function") {
  122. throw new TypeError("The last argument to .catch() " +
  123. "must be a function, got " + util.toString(fn));
  124. }
  125. return this.then(undefined, catchFilter(catchInstances, fn, this));
  126. }
  127. return this.then(undefined, fn);
  128. };
  129. Promise.prototype.reflect = function () {
  130. return this._then(reflectHandler,
  131. reflectHandler, undefined, this, undefined);
  132. };
  133. Promise.prototype.then = function (didFulfill, didReject) {
  134. if (debug.warnings() && arguments.length > 0 &&
  135. typeof didFulfill !== "function" &&
  136. typeof didReject !== "function") {
  137. var msg = ".then() only accepts functions but was passed: " +
  138. util.classString(didFulfill);
  139. if (arguments.length > 1) {
  140. msg += ", " + util.classString(didReject);
  141. }
  142. this._warn(msg);
  143. }
  144. return this._then(didFulfill, didReject, undefined, undefined, undefined);
  145. };
  146. Promise.prototype.done = function (didFulfill, didReject) {
  147. var promise =
  148. this._then(didFulfill, didReject, undefined, undefined, undefined);
  149. promise._setIsFinal();
  150. };
  151. Promise.prototype.spread = function (fn) {
  152. if (typeof fn !== "function") {
  153. return apiRejection("expecting a function but got " + util.classString(fn));
  154. }
  155. return this.all()._then(fn, undefined, undefined, APPLY, undefined);
  156. };
  157. Promise.prototype.toJSON = function () {
  158. var ret = {
  159. isFulfilled: false,
  160. isRejected: false,
  161. fulfillmentValue: undefined,
  162. rejectionReason: undefined
  163. };
  164. if (this.isFulfilled()) {
  165. ret.fulfillmentValue = this.value();
  166. ret.isFulfilled = true;
  167. } else if (this.isRejected()) {
  168. ret.rejectionReason = this.reason();
  169. ret.isRejected = true;
  170. }
  171. return ret;
  172. };
  173. Promise.prototype.all = function () {
  174. if (arguments.length > 0) {
  175. this._warn(".all() was passed arguments but it does not take any");
  176. }
  177. return new PromiseArray(this).promise();
  178. };
  179. Promise.prototype.error = function (fn) {
  180. return this.caught(util.originatesFromRejection, fn);
  181. };
  182. Promise.getNewLibraryCopy = module.exports;
  183. Promise.is = function (val) {
  184. return val instanceof Promise;
  185. };
  186. Promise.fromNode = Promise.fromCallback = function(fn) {
  187. var ret = new Promise(INTERNAL);
  188. ret._captureStackTrace();
  189. var multiArgs = arguments.length > 1 ? !!Object(arguments[1]).multiArgs
  190. : false;
  191. var result = tryCatch(fn)(nodebackForPromise(ret, multiArgs));
  192. if (result === errorObj) {
  193. ret._rejectCallback(result.e, true);
  194. }
  195. if (!ret._isFateSealed()) ret._setAsyncGuaranteed();
  196. return ret;
  197. };
  198. Promise.all = function (promises) {
  199. return new PromiseArray(promises).promise();
  200. };
  201. Promise.cast = function (obj) {
  202. var ret = tryConvertToPromise(obj);
  203. if (!(ret instanceof Promise)) {
  204. ret = new Promise(INTERNAL);
  205. ret._captureStackTrace();
  206. ret._setFulfilled();
  207. ret._rejectionHandler0 = obj;
  208. }
  209. return ret;
  210. };
  211. Promise.resolve = Promise.fulfilled = Promise.cast;
  212. Promise.reject = Promise.rejected = function (reason) {
  213. var ret = new Promise(INTERNAL);
  214. ret._captureStackTrace();
  215. ret._rejectCallback(reason, true);
  216. return ret;
  217. };
  218. Promise.setScheduler = function(fn) {
  219. if (typeof fn !== "function") {
  220. throw new TypeError("expecting a function but got " + util.classString(fn));
  221. }
  222. return async.setScheduler(fn);
  223. };
  224. Promise.prototype._then = function (
  225. didFulfill,
  226. didReject,
  227. _, receiver,
  228. internalData
  229. ) {
  230. var haveInternalData = internalData !== undefined;
  231. var promise = haveInternalData ? internalData : new Promise(INTERNAL);
  232. var target = this._target();
  233. var bitField = target._bitField;
  234. if (!haveInternalData) {
  235. promise._propagateFrom(this, 3);
  236. promise._captureStackTrace();
  237. if (receiver === undefined &&
  238. ((this._bitField & 2097152) !== 0)) {
  239. if (!((bitField & 50397184) === 0)) {
  240. receiver = this._boundValue();
  241. } else {
  242. receiver = target === this ? undefined : this._boundTo;
  243. }
  244. }
  245. this._fireEvent("promiseChained", this, promise);
  246. }
  247. var context = getContext();
  248. if (!((bitField & 50397184) === 0)) {
  249. var handler, value, settler = target._settlePromiseCtx;
  250. if (((bitField & 33554432) !== 0)) {
  251. value = target._rejectionHandler0;
  252. handler = didFulfill;
  253. } else if (((bitField & 16777216) !== 0)) {
  254. value = target._fulfillmentHandler0;
  255. handler = didReject;
  256. target._unsetRejectionIsUnhandled();
  257. } else {
  258. settler = target._settlePromiseLateCancellationObserver;
  259. value = new CancellationError("late cancellation observer");
  260. target._attachExtraTrace(value);
  261. handler = didReject;
  262. }
  263. async.invoke(settler, target, {
  264. handler: util.contextBind(context, handler),
  265. promise: promise,
  266. receiver: receiver,
  267. value: value
  268. });
  269. } else {
  270. target._addCallbacks(didFulfill, didReject, promise,
  271. receiver, context);
  272. }
  273. return promise;
  274. };
  275. Promise.prototype._length = function () {
  276. return this._bitField & 65535;
  277. };
  278. Promise.prototype._isFateSealed = function () {
  279. return (this._bitField & 117506048) !== 0;
  280. };
  281. Promise.prototype._isFollowing = function () {
  282. return (this._bitField & 67108864) === 67108864;
  283. };
  284. Promise.prototype._setLength = function (len) {
  285. this._bitField = (this._bitField & -65536) |
  286. (len & 65535);
  287. };
  288. Promise.prototype._setFulfilled = function () {
  289. this._bitField = this._bitField | 33554432;
  290. this._fireEvent("promiseFulfilled", this);
  291. };
  292. Promise.prototype._setRejected = function () {
  293. this._bitField = this._bitField | 16777216;
  294. this._fireEvent("promiseRejected", this);
  295. };
  296. Promise.prototype._setFollowing = function () {
  297. this._bitField = this._bitField | 67108864;
  298. this._fireEvent("promiseResolved", this);
  299. };
  300. Promise.prototype._setIsFinal = function () {
  301. this._bitField = this._bitField | 4194304;
  302. };
  303. Promise.prototype._isFinal = function () {
  304. return (this._bitField & 4194304) > 0;
  305. };
  306. Promise.prototype._unsetCancelled = function() {
  307. this._bitField = this._bitField & (~65536);
  308. };
  309. Promise.prototype._setCancelled = function() {
  310. this._bitField = this._bitField | 65536;
  311. this._fireEvent("promiseCancelled", this);
  312. };
  313. Promise.prototype._setWillBeCancelled = function() {
  314. this._bitField = this._bitField | 8388608;
  315. };
  316. Promise.prototype._setAsyncGuaranteed = function() {
  317. if (async.hasCustomScheduler()) return;
  318. var bitField = this._bitField;
  319. this._bitField = bitField |
  320. (((bitField & 536870912) >> 2) ^
  321. 134217728);
  322. };
  323. Promise.prototype._setNoAsyncGuarantee = function() {
  324. this._bitField = (this._bitField | 536870912) &
  325. (~134217728);
  326. };
  327. Promise.prototype._receiverAt = function (index) {
  328. var ret = index === 0 ? this._receiver0 : this[
  329. index * 4 - 4 + 3];
  330. if (ret === UNDEFINED_BINDING) {
  331. return undefined;
  332. } else if (ret === undefined && this._isBound()) {
  333. return this._boundValue();
  334. }
  335. return ret;
  336. };
  337. Promise.prototype._promiseAt = function (index) {
  338. return this[
  339. index * 4 - 4 + 2];
  340. };
  341. Promise.prototype._fulfillmentHandlerAt = function (index) {
  342. return this[
  343. index * 4 - 4 + 0];
  344. };
  345. Promise.prototype._rejectionHandlerAt = function (index) {
  346. return this[
  347. index * 4 - 4 + 1];
  348. };
  349. Promise.prototype._boundValue = function() {};
  350. Promise.prototype._migrateCallback0 = function (follower) {
  351. var bitField = follower._bitField;
  352. var fulfill = follower._fulfillmentHandler0;
  353. var reject = follower._rejectionHandler0;
  354. var promise = follower._promise0;
  355. var receiver = follower._receiverAt(0);
  356. if (receiver === undefined) receiver = UNDEFINED_BINDING;
  357. this._addCallbacks(fulfill, reject, promise, receiver, null);
  358. };
  359. Promise.prototype._migrateCallbackAt = function (follower, index) {
  360. var fulfill = follower._fulfillmentHandlerAt(index);
  361. var reject = follower._rejectionHandlerAt(index);
  362. var promise = follower._promiseAt(index);
  363. var receiver = follower._receiverAt(index);
  364. if (receiver === undefined) receiver = UNDEFINED_BINDING;
  365. this._addCallbacks(fulfill, reject, promise, receiver, null);
  366. };
  367. Promise.prototype._addCallbacks = function (
  368. fulfill,
  369. reject,
  370. promise,
  371. receiver,
  372. context
  373. ) {
  374. var index = this._length();
  375. if (index >= 65535 - 4) {
  376. index = 0;
  377. this._setLength(0);
  378. }
  379. if (index === 0) {
  380. this._promise0 = promise;
  381. this._receiver0 = receiver;
  382. if (typeof fulfill === "function") {
  383. this._fulfillmentHandler0 = util.contextBind(context, fulfill);
  384. }
  385. if (typeof reject === "function") {
  386. this._rejectionHandler0 = util.contextBind(context, reject);
  387. }
  388. } else {
  389. var base = index * 4 - 4;
  390. this[base + 2] = promise;
  391. this[base + 3] = receiver;
  392. if (typeof fulfill === "function") {
  393. this[base + 0] =
  394. util.contextBind(context, fulfill);
  395. }
  396. if (typeof reject === "function") {
  397. this[base + 1] =
  398. util.contextBind(context, reject);
  399. }
  400. }
  401. this._setLength(index + 1);
  402. return index;
  403. };
  404. Promise.prototype._proxy = function (proxyable, arg) {
  405. this._addCallbacks(undefined, undefined, arg, proxyable, null);
  406. };
  407. Promise.prototype._resolveCallback = function(value, shouldBind) {
  408. if (((this._bitField & 117506048) !== 0)) return;
  409. if (value === this)
  410. return this._rejectCallback(makeSelfResolutionError(), false);
  411. var maybePromise = tryConvertToPromise(value, this);
  412. if (!(maybePromise instanceof Promise)) return this._fulfill(value);
  413. if (shouldBind) this._propagateFrom(maybePromise, 2);
  414. var promise = maybePromise._target();
  415. if (promise === this) {
  416. this._reject(makeSelfResolutionError());
  417. return;
  418. }
  419. var bitField = promise._bitField;
  420. if (((bitField & 50397184) === 0)) {
  421. var len = this._length();
  422. if (len > 0) promise._migrateCallback0(this);
  423. for (var i = 1; i < len; ++i) {
  424. promise._migrateCallbackAt(this, i);
  425. }
  426. this._setFollowing();
  427. this._setLength(0);
  428. this._setFollowee(maybePromise);
  429. } else if (((bitField & 33554432) !== 0)) {
  430. this._fulfill(promise._value());
  431. } else if (((bitField & 16777216) !== 0)) {
  432. this._reject(promise._reason());
  433. } else {
  434. var reason = new CancellationError("late cancellation observer");
  435. promise._attachExtraTrace(reason);
  436. this._reject(reason);
  437. }
  438. };
  439. Promise.prototype._rejectCallback =
  440. function(reason, synchronous, ignoreNonErrorWarnings) {
  441. var trace = util.ensureErrorObject(reason);
  442. var hasStack = trace === reason;
  443. if (!hasStack && !ignoreNonErrorWarnings && debug.warnings()) {
  444. var message = "a promise was rejected with a non-error: " +
  445. util.classString(reason);
  446. this._warn(message, true);
  447. }
  448. this._attachExtraTrace(trace, synchronous ? hasStack : false);
  449. this._reject(reason);
  450. };
  451. Promise.prototype._resolveFromExecutor = function (executor) {
  452. if (executor === INTERNAL) return;
  453. var promise = this;
  454. this._captureStackTrace();
  455. this._pushContext();
  456. var synchronous = true;
  457. var r = this._execute(executor, function(value) {
  458. promise._resolveCallback(value);
  459. }, function (reason) {
  460. promise._rejectCallback(reason, synchronous);
  461. });
  462. synchronous = false;
  463. this._popContext();
  464. if (r !== undefined) {
  465. promise._rejectCallback(r, true);
  466. }
  467. };
  468. Promise.prototype._settlePromiseFromHandler = function (
  469. handler, receiver, value, promise
  470. ) {
  471. var bitField = promise._bitField;
  472. if (((bitField & 65536) !== 0)) return;
  473. promise._pushContext();
  474. var x;
  475. if (receiver === APPLY) {
  476. if (!value || typeof value.length !== "number") {
  477. x = errorObj;
  478. x.e = new TypeError("cannot .spread() a non-array: " +
  479. util.classString(value));
  480. } else {
  481. x = tryCatch(handler).apply(this._boundValue(), value);
  482. }
  483. } else {
  484. x = tryCatch(handler).call(receiver, value);
  485. }
  486. var promiseCreated = promise._popContext();
  487. bitField = promise._bitField;
  488. if (((bitField & 65536) !== 0)) return;
  489. if (x === NEXT_FILTER) {
  490. promise._reject(value);
  491. } else if (x === errorObj) {
  492. promise._rejectCallback(x.e, false);
  493. } else {
  494. debug.checkForgottenReturns(x, promiseCreated, "", promise, this);
  495. promise._resolveCallback(x);
  496. }
  497. };
  498. Promise.prototype._target = function() {
  499. var ret = this;
  500. while (ret._isFollowing()) ret = ret._followee();
  501. return ret;
  502. };
  503. Promise.prototype._followee = function() {
  504. return this._rejectionHandler0;
  505. };
  506. Promise.prototype._setFollowee = function(promise) {
  507. this._rejectionHandler0 = promise;
  508. };
  509. Promise.prototype._settlePromise = function(promise, handler, receiver, value) {
  510. var isPromise = promise instanceof Promise;
  511. var bitField = this._bitField;
  512. var asyncGuaranteed = ((bitField & 134217728) !== 0);
  513. if (((bitField & 65536) !== 0)) {
  514. if (isPromise) promise._invokeInternalOnCancel();
  515. if (receiver instanceof PassThroughHandlerContext &&
  516. receiver.isFinallyHandler()) {
  517. receiver.cancelPromise = promise;
  518. if (tryCatch(handler).call(receiver, value) === errorObj) {
  519. promise._reject(errorObj.e);
  520. }
  521. } else if (handler === reflectHandler) {
  522. promise._fulfill(reflectHandler.call(receiver));
  523. } else if (receiver instanceof Proxyable) {
  524. receiver._promiseCancelled(promise);
  525. } else if (isPromise || promise instanceof PromiseArray) {
  526. promise._cancel();
  527. } else {
  528. receiver.cancel();
  529. }
  530. } else if (typeof handler === "function") {
  531. if (!isPromise) {
  532. handler.call(receiver, value, promise);
  533. } else {
  534. if (asyncGuaranteed) promise._setAsyncGuaranteed();
  535. this._settlePromiseFromHandler(handler, receiver, value, promise);
  536. }
  537. } else if (receiver instanceof Proxyable) {
  538. if (!receiver._isResolved()) {
  539. if (((bitField & 33554432) !== 0)) {
  540. receiver._promiseFulfilled(value, promise);
  541. } else {
  542. receiver._promiseRejected(value, promise);
  543. }
  544. }
  545. } else if (isPromise) {
  546. if (asyncGuaranteed) promise._setAsyncGuaranteed();
  547. if (((bitField & 33554432) !== 0)) {
  548. promise._fulfill(value);
  549. } else {
  550. promise._reject(value);
  551. }
  552. }
  553. };
  554. Promise.prototype._settlePromiseLateCancellationObserver = function(ctx) {
  555. var handler = ctx.handler;
  556. var promise = ctx.promise;
  557. var receiver = ctx.receiver;
  558. var value = ctx.value;
  559. if (typeof handler === "function") {
  560. if (!(promise instanceof Promise)) {
  561. handler.call(receiver, value, promise);
  562. } else {
  563. this._settlePromiseFromHandler(handler, receiver, value, promise);
  564. }
  565. } else if (promise instanceof Promise) {
  566. promise._reject(value);
  567. }
  568. };
  569. Promise.prototype._settlePromiseCtx = function(ctx) {
  570. this._settlePromise(ctx.promise, ctx.handler, ctx.receiver, ctx.value);
  571. };
  572. Promise.prototype._settlePromise0 = function(handler, value, bitField) {
  573. var promise = this._promise0;
  574. var receiver = this._receiverAt(0);
  575. this._promise0 = undefined;
  576. this._receiver0 = undefined;
  577. this._settlePromise(promise, handler, receiver, value);
  578. };
  579. Promise.prototype._clearCallbackDataAtIndex = function(index) {
  580. var base = index * 4 - 4;
  581. this[base + 2] =
  582. this[base + 3] =
  583. this[base + 0] =
  584. this[base + 1] = undefined;
  585. };
  586. Promise.prototype._fulfill = function (value) {
  587. var bitField = this._bitField;
  588. if (((bitField & 117506048) >>> 16)) return;
  589. if (value === this) {
  590. var err = makeSelfResolutionError();
  591. this._attachExtraTrace(err);
  592. return this._reject(err);
  593. }
  594. this._setFulfilled();
  595. this._rejectionHandler0 = value;
  596. if ((bitField & 65535) > 0) {
  597. if (((bitField & 134217728) !== 0)) {
  598. this._settlePromises();
  599. } else {
  600. async.settlePromises(this);
  601. }
  602. this._dereferenceTrace();
  603. }
  604. };
  605. Promise.prototype._reject = function (reason) {
  606. var bitField = this._bitField;
  607. if (((bitField & 117506048) >>> 16)) return;
  608. this._setRejected();
  609. this._fulfillmentHandler0 = reason;
  610. if (this._isFinal()) {
  611. return async.fatalError(reason, util.isNode);
  612. }
  613. if ((bitField & 65535) > 0) {
  614. async.settlePromises(this);
  615. } else {
  616. this._ensurePossibleRejectionHandled();
  617. }
  618. };
  619. Promise.prototype._fulfillPromises = function (len, value) {
  620. for (var i = 1; i < len; i++) {
  621. var handler = this._fulfillmentHandlerAt(i);
  622. var promise = this._promiseAt(i);
  623. var receiver = this._receiverAt(i);
  624. this._clearCallbackDataAtIndex(i);
  625. this._settlePromise(promise, handler, receiver, value);
  626. }
  627. };
  628. Promise.prototype._rejectPromises = function (len, reason) {
  629. for (var i = 1; i < len; i++) {
  630. var handler = this._rejectionHandlerAt(i);
  631. var promise = this._promiseAt(i);
  632. var receiver = this._receiverAt(i);
  633. this._clearCallbackDataAtIndex(i);
  634. this._settlePromise(promise, handler, receiver, reason);
  635. }
  636. };
  637. Promise.prototype._settlePromises = function () {
  638. var bitField = this._bitField;
  639. var len = (bitField & 65535);
  640. if (len > 0) {
  641. if (((bitField & 16842752) !== 0)) {
  642. var reason = this._fulfillmentHandler0;
  643. this._settlePromise0(this._rejectionHandler0, reason, bitField);
  644. this._rejectPromises(len, reason);
  645. } else {
  646. var value = this._rejectionHandler0;
  647. this._settlePromise0(this._fulfillmentHandler0, value, bitField);
  648. this._fulfillPromises(len, value);
  649. }
  650. this._setLength(0);
  651. }
  652. this._clearCancellationData();
  653. };
  654. Promise.prototype._settledValue = function() {
  655. var bitField = this._bitField;
  656. if (((bitField & 33554432) !== 0)) {
  657. return this._rejectionHandler0;
  658. } else if (((bitField & 16777216) !== 0)) {
  659. return this._fulfillmentHandler0;
  660. }
  661. };
  662. if (typeof Symbol !== "undefined" && Symbol.toStringTag) {
  663. es5.defineProperty(Promise.prototype, Symbol.toStringTag, {
  664. get: function () {
  665. return "Object";
  666. }
  667. });
  668. }
  669. function deferResolve(v) {this.promise._resolveCallback(v);}
  670. function deferReject(v) {this.promise._rejectCallback(v, false);}
  671. Promise.defer = Promise.pending = function() {
  672. debug.deprecated("Promise.defer", "new Promise");
  673. var promise = new Promise(INTERNAL);
  674. return {
  675. promise: promise,
  676. resolve: deferResolve,
  677. reject: deferReject
  678. };
  679. };
  680. util.notEnumerableProp(Promise,
  681. "_makeSelfResolutionError",
  682. makeSelfResolutionError);
  683. require("./method")(Promise, INTERNAL, tryConvertToPromise, apiRejection,
  684. debug);
  685. require("./bind")(Promise, INTERNAL, tryConvertToPromise, debug);
  686. require("./cancel")(Promise, PromiseArray, apiRejection, debug);
  687. require("./direct_resolve")(Promise);
  688. require("./synchronous_inspection")(Promise);
  689. require("./join")(
  690. Promise, PromiseArray, tryConvertToPromise, INTERNAL, async);
  691. Promise.Promise = Promise;
  692. Promise.version = "3.7.2";
  693. require('./call_get.js')(Promise);
  694. require('./generators.js')(Promise, apiRejection, INTERNAL, tryConvertToPromise, Proxyable, debug);
  695. require('./map.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL, debug);
  696. require('./nodeify.js')(Promise);
  697. require('./promisify.js')(Promise, INTERNAL);
  698. require('./props.js')(Promise, PromiseArray, tryConvertToPromise, apiRejection);
  699. require('./race.js')(Promise, INTERNAL, tryConvertToPromise, apiRejection);
  700. require('./reduce.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL, debug);
  701. require('./settle.js')(Promise, PromiseArray, debug);
  702. require('./some.js')(Promise, PromiseArray, apiRejection);
  703. require('./timers.js')(Promise, INTERNAL, debug);
  704. require('./using.js')(Promise, apiRejection, tryConvertToPromise, createContext, INTERNAL, debug);
  705. require('./any.js')(Promise);
  706. require('./each.js')(Promise, INTERNAL);
  707. require('./filter.js')(Promise, INTERNAL);
  708. util.toFastProperties(Promise);
  709. util.toFastProperties(Promise.prototype);
  710. function fillTypes(value) {
  711. var p = new Promise(INTERNAL);
  712. p._fulfillmentHandler0 = value;
  713. p._rejectionHandler0 = value;
  714. p._promise0 = value;
  715. p._receiver0 = value;
  716. }
  717. // Complete slack tracking, opt out of field-type tracking and
  718. // stabilize map
  719. fillTypes({a: 1});
  720. fillTypes({b: 2});
  721. fillTypes({c: 3});
  722. fillTypes(1);
  723. fillTypes(function(){});
  724. fillTypes(undefined);
  725. fillTypes(false);
  726. fillTypes(new Promise(INTERNAL));
  727. debug.setBounds(Async.firstLineError, util.lastLineError);
  728. return Promise;
  729. };