Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

promise.js 26KB

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