promise.js 25 KB

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