2 module.exports = function() {
3 var makeSelfResolutionError = function () {
4 return new TypeError("circular promise resolution chain\u000a\u000a See http://goo.gl/LhFpo0\u000a");
6 var reflect = function() {
7 return new Promise.PromiseInspection(this._target());
9 var apiRejection = function(msg) {
10 return Promise.reject(new TypeError(msg));
13 var util = require("./util.js");
17 getDomain = function() {
18 var ret = process.domain;
19 if (ret === undefined) ret = null;
23 getDomain = function() {
27 util.notEnumerableProp(Promise, "_getDomain", getDomain);
29 var UNDEFINED_BINDING = {};
30 var async = require("./async.js");
31 var errors = require("./errors.js");
32 var TypeError = Promise.TypeError = errors.TypeError;
33 Promise.RangeError = errors.RangeError;
34 Promise.CancellationError = errors.CancellationError;
35 Promise.TimeoutError = errors.TimeoutError;
36 Promise.OperationalError = errors.OperationalError;
37 Promise.RejectionError = errors.OperationalError;
38 Promise.AggregateError = errors.AggregateError;
39 var INTERNAL = function(){};
41 var NEXT_FILTER = {e: null};
42 var tryConvertToPromise = require("./thenables.js")(Promise, INTERNAL);
44 require("./promise_array.js")(Promise, INTERNAL,
45 tryConvertToPromise, apiRejection);
46 var CapturedTrace = require("./captured_trace.js")();
47 var isDebugging = require("./debuggability.js")(Promise, CapturedTrace);
48 /*jshint unused:false*/
50 require("./context.js")(Promise, CapturedTrace, isDebugging);
51 var CatchFilter = require("./catch_filter.js")(NEXT_FILTER);
52 var PromiseResolver = require("./promise_resolver.js");
53 var nodebackForPromise = PromiseResolver._nodebackForPromise;
54 var errorObj = util.errorObj;
55 var tryCatch = util.tryCatch;
56 function Promise(resolver) {
57 if (typeof resolver !== "function") {
58 throw new TypeError("the promise constructor requires a resolver function\u000a\u000a See http://goo.gl/EC22Yn\u000a");
60 if (this.constructor !== Promise) {
61 throw new TypeError("the promise constructor cannot be invoked directly\u000a\u000a See http://goo.gl/KsIlge\u000a");
64 this._fulfillmentHandler0 = undefined;
65 this._rejectionHandler0 = undefined;
66 this._progressHandler0 = undefined;
67 this._promise0 = undefined;
68 this._receiver0 = undefined;
69 this._settledValue = undefined;
70 if (resolver !== INTERNAL) this._resolveFromResolver(resolver);
73 Promise.prototype.toString = function () {
74 return "[object Promise]";
77 Promise.prototype.caught = Promise.prototype["catch"] = function (fn) {
78 var len = arguments.length;
80 var catchInstances = new Array(len - 1),
82 for (i = 0; i < len - 1; ++i) {
83 var item = arguments[i];
84 if (typeof item === "function") {
85 catchInstances[j++] = item;
87 return Promise.reject(
88 new TypeError("Catch filter must inherit from Error or be a simple predicate function\u000a\u000a See http://goo.gl/o84o68\u000a"));
91 catchInstances.length = j;
93 var catchFilter = new CatchFilter(catchInstances, fn, this);
94 return this._then(undefined, catchFilter.doFilter, undefined,
95 catchFilter, undefined);
97 return this._then(undefined, fn, undefined, undefined, undefined);
100 Promise.prototype.reflect = function () {
101 return this._then(reflect, reflect, undefined, this, undefined);
104 Promise.prototype.then = function (didFulfill, didReject, didProgress) {
105 if (isDebugging() && arguments.length > 0 &&
106 typeof didFulfill !== "function" &&
107 typeof didReject !== "function") {
108 var msg = ".then() only accepts functions but was passed: " +
109 util.classString(didFulfill);
110 if (arguments.length > 1) {
111 msg += ", " + util.classString(didReject);
115 return this._then(didFulfill, didReject, didProgress,
116 undefined, undefined);
119 Promise.prototype.done = function (didFulfill, didReject, didProgress) {
120 var promise = this._then(didFulfill, didReject, didProgress,
121 undefined, undefined);
122 promise._setIsFinal();
125 Promise.prototype.spread = function (didFulfill, didReject) {
126 return this.all()._then(didFulfill, didReject, undefined, APPLY, undefined);
129 Promise.prototype.isCancellable = function () {
130 return !this.isResolved() &&
134 Promise.prototype.toJSON = function () {
138 fulfillmentValue: undefined,
139 rejectionReason: undefined
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;
151 Promise.prototype.all = function () {
152 return new PromiseArray(this).promise();
155 Promise.prototype.error = function (fn) {
156 return this.caught(util.originatesFromRejection, fn);
159 Promise.is = function (val) {
160 return val instanceof Promise;
163 Promise.fromNode = function(fn) {
164 var ret = new Promise(INTERNAL);
165 var result = tryCatch(fn)(nodebackForPromise(ret));
166 if (result === errorObj) {
167 ret._rejectCallback(result.e, true, true);
172 Promise.all = function (promises) {
173 return new PromiseArray(promises).promise();
176 Promise.defer = Promise.pending = function () {
177 var promise = new Promise(INTERNAL);
178 return new PromiseResolver(promise);
181 Promise.cast = function (obj) {
182 var ret = tryConvertToPromise(obj);
183 if (!(ret instanceof Promise)) {
185 ret = new Promise(INTERNAL);
186 ret._fulfillUnchecked(val);
191 Promise.resolve = Promise.fulfilled = Promise.cast;
193 Promise.reject = Promise.rejected = function (reason) {
194 var ret = new Promise(INTERNAL);
195 ret._captureStackTrace();
196 ret._rejectCallback(reason, true);
200 Promise.setScheduler = function(fn) {
201 if (typeof fn !== "function") throw new TypeError("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
202 var prev = async._schedule;
203 async._schedule = fn;
207 Promise.prototype._then = function (
214 var haveInternalData = internalData !== undefined;
215 var ret = haveInternalData ? internalData : new Promise(INTERNAL);
217 if (!haveInternalData) {
218 ret._propagateFrom(this, 4 | 1);
219 ret._captureStackTrace();
222 var target = this._target();
223 if (target !== this) {
224 if (receiver === undefined) receiver = this._boundTo;
225 if (!haveInternalData) ret._setIsMigrated();
228 var callbackIndex = target._addCallbacks(didFulfill,
235 if (target._isResolved() && !target._isSettlePromisesQueued()) {
237 target._settlePromiseAtPostResolution, target, callbackIndex);
243 Promise.prototype._settlePromiseAtPostResolution = function (index) {
244 if (this._isRejectionUnhandled()) this._unsetRejectionIsUnhandled();
245 this._settlePromiseAt(index);
248 Promise.prototype._length = function () {
249 return this._bitField & 131071;
252 Promise.prototype._isFollowingOrFulfilledOrRejected = function () {
253 return (this._bitField & 939524096) > 0;
256 Promise.prototype._isFollowing = function () {
257 return (this._bitField & 536870912) === 536870912;
260 Promise.prototype._setLength = function (len) {
261 this._bitField = (this._bitField & -131072) |
265 Promise.prototype._setFulfilled = function () {
266 this._bitField = this._bitField | 268435456;
269 Promise.prototype._setRejected = function () {
270 this._bitField = this._bitField | 134217728;
273 Promise.prototype._setFollowing = function () {
274 this._bitField = this._bitField | 536870912;
277 Promise.prototype._setIsFinal = function () {
278 this._bitField = this._bitField | 33554432;
281 Promise.prototype._isFinal = function () {
282 return (this._bitField & 33554432) > 0;
285 Promise.prototype._cancellable = function () {
286 return (this._bitField & 67108864) > 0;
289 Promise.prototype._setCancellable = function () {
290 this._bitField = this._bitField | 67108864;
293 Promise.prototype._unsetCancellable = function () {
294 this._bitField = this._bitField & (~67108864);
297 Promise.prototype._setIsMigrated = function () {
298 this._bitField = this._bitField | 4194304;
301 Promise.prototype._unsetIsMigrated = function () {
302 this._bitField = this._bitField & (~4194304);
305 Promise.prototype._isMigrated = function () {
306 return (this._bitField & 4194304) > 0;
309 Promise.prototype._receiverAt = function (index) {
310 var ret = index === 0
314 if (ret === UNDEFINED_BINDING) {
316 } else if (ret === undefined && this._isBound()) {
317 return this._boundValue();
322 Promise.prototype._promiseAt = function (index) {
325 : this[index * 5 - 5 + 3];
328 Promise.prototype._fulfillmentHandlerAt = function (index) {
330 ? this._fulfillmentHandler0
331 : this[index * 5 - 5 + 0];
334 Promise.prototype._rejectionHandlerAt = function (index) {
336 ? this._rejectionHandler0
337 : this[index * 5 - 5 + 1];
340 Promise.prototype._boundValue = function() {
341 var ret = this._boundTo;
342 if (ret !== undefined) {
343 if (ret instanceof Promise) {
344 if (ret.isFulfilled()) {
354 Promise.prototype._migrateCallbacks = function (follower, index) {
355 var fulfill = follower._fulfillmentHandlerAt(index);
356 var reject = follower._rejectionHandlerAt(index);
357 var progress = follower._progressHandlerAt(index);
358 var promise = follower._promiseAt(index);
359 var receiver = follower._receiverAt(index);
360 if (promise instanceof Promise) promise._setIsMigrated();
361 if (receiver === undefined) receiver = UNDEFINED_BINDING;
362 this._addCallbacks(fulfill, reject, progress, promise, receiver, null);
365 Promise.prototype._addCallbacks = function (
373 var index = this._length();
375 if (index >= 131071 - 5) {
381 this._promise0 = promise;
382 if (receiver !== undefined) this._receiver0 = receiver;
383 if (typeof fulfill === "function" && !this._isCarryingStackTrace()) {
384 this._fulfillmentHandler0 =
385 domain === null ? fulfill : domain.bind(fulfill);
387 if (typeof reject === "function") {
388 this._rejectionHandler0 =
389 domain === null ? reject : domain.bind(reject);
391 if (typeof progress === "function") {
392 this._progressHandler0 =
393 domain === null ? progress : domain.bind(progress);
396 var base = index * 5 - 5;
397 this[base + 3] = promise;
398 this[base + 4] = receiver;
399 if (typeof fulfill === "function") {
401 domain === null ? fulfill : domain.bind(fulfill);
403 if (typeof reject === "function") {
405 domain === null ? reject : domain.bind(reject);
407 if (typeof progress === "function") {
409 domain === null ? progress : domain.bind(progress);
412 this._setLength(index + 1);
416 Promise.prototype._setProxyHandlers = function (receiver, promiseSlotValue) {
417 var index = this._length();
419 if (index >= 131071 - 5) {
424 this._promise0 = promiseSlotValue;
425 this._receiver0 = receiver;
427 var base = index * 5 - 5;
428 this[base + 3] = promiseSlotValue;
429 this[base + 4] = receiver;
431 this._setLength(index + 1);
434 Promise.prototype._proxyPromiseArray = function (promiseArray, index) {
435 this._setProxyHandlers(promiseArray, index);
438 Promise.prototype._resolveCallback = function(value, shouldBind) {
439 if (this._isFollowingOrFulfilledOrRejected()) return;
441 return this._rejectCallback(makeSelfResolutionError(), false, true);
442 var maybePromise = tryConvertToPromise(value, this);
443 if (!(maybePromise instanceof Promise)) return this._fulfill(value);
445 var propagationFlags = 1 | (shouldBind ? 4 : 0);
446 this._propagateFrom(maybePromise, propagationFlags);
447 var promise = maybePromise._target();
448 if (promise._isPending()) {
449 var len = this._length();
450 for (var i = 0; i < len; ++i) {
451 promise._migrateCallbacks(this, i);
453 this._setFollowing();
455 this._setFollowee(promise);
456 } else if (promise._isFulfilled()) {
457 this._fulfillUnchecked(promise._value());
459 this._rejectUnchecked(promise._reason(),
460 promise._getCarriedStackTrace());
464 Promise.prototype._rejectCallback =
465 function(reason, synchronous, shouldNotMarkOriginatingFromRejection) {
466 if (!shouldNotMarkOriginatingFromRejection) {
467 util.markAsOriginatingFromRejection(reason);
469 var trace = util.ensureErrorObject(reason);
470 var hasStack = trace === reason;
471 this._attachExtraTrace(trace, synchronous ? hasStack : false);
472 this._reject(reason, hasStack ? undefined : trace);
475 Promise.prototype._resolveFromResolver = function (resolver) {
477 this._captureStackTrace();
479 var synchronous = true;
480 var r = tryCatch(resolver)(function(value) {
481 if (promise === null) return;
482 promise._resolveCallback(value);
484 }, function (reason) {
485 if (promise === null) return;
486 promise._rejectCallback(reason, synchronous);
492 if (r !== undefined && r === errorObj && promise !== null) {
493 promise._rejectCallback(r.e, true, true);
498 Promise.prototype._settlePromiseFromHandler = function (
499 handler, receiver, value, promise
501 if (promise._isRejected()) return;
502 promise._pushContext();
504 if (receiver === APPLY && !this._isRejected()) {
505 x = tryCatch(handler).apply(this._boundValue(), value);
507 x = tryCatch(handler).call(receiver, value);
509 promise._popContext();
511 if (x === errorObj || x === promise || x === NEXT_FILTER) {
512 var err = x === promise ? makeSelfResolutionError() : x.e;
513 promise._rejectCallback(err, false, true);
515 promise._resolveCallback(x);
519 Promise.prototype._target = function() {
521 while (ret._isFollowing()) ret = ret._followee();
525 Promise.prototype._followee = function() {
526 return this._rejectionHandler0;
529 Promise.prototype._setFollowee = function(promise) {
530 this._rejectionHandler0 = promise;
533 Promise.prototype._cleanValues = function () {
534 if (this._cancellable()) {
535 this._cancellationParent = undefined;
539 Promise.prototype._propagateFrom = function (parent, flags) {
540 if ((flags & 1) > 0 && parent._cancellable()) {
541 this._setCancellable();
542 this._cancellationParent = parent;
544 if ((flags & 4) > 0 && parent._isBound()) {
545 this._setBoundTo(parent._boundTo);
549 Promise.prototype._fulfill = function (value) {
550 if (this._isFollowingOrFulfilledOrRejected()) return;
551 this._fulfillUnchecked(value);
554 Promise.prototype._reject = function (reason, carriedStackTrace) {
555 if (this._isFollowingOrFulfilledOrRejected()) return;
556 this._rejectUnchecked(reason, carriedStackTrace);
559 Promise.prototype._settlePromiseAt = function (index) {
560 var promise = this._promiseAt(index);
561 var isPromise = promise instanceof Promise;
563 if (isPromise && promise._isMigrated()) {
564 promise._unsetIsMigrated();
565 return async.invoke(this._settlePromiseAt, this, index);
567 var handler = this._isFulfilled()
568 ? this._fulfillmentHandlerAt(index)
569 : this._rejectionHandlerAt(index);
571 var carriedStackTrace =
572 this._isCarryingStackTrace() ? this._getCarriedStackTrace() : undefined;
573 var value = this._settledValue;
574 var receiver = this._receiverAt(index);
575 this._clearCallbackDataAtIndex(index);
577 if (typeof handler === "function") {
579 handler.call(receiver, value, promise);
581 this._settlePromiseFromHandler(handler, receiver, value, promise);
583 } else if (receiver instanceof PromiseArray) {
584 if (!receiver._isResolved()) {
585 if (this._isFulfilled()) {
586 receiver._promiseFulfilled(value, promise);
589 receiver._promiseRejected(value, promise);
592 } else if (isPromise) {
593 if (this._isFulfilled()) {
594 promise._fulfill(value);
596 promise._reject(value, carriedStackTrace);
600 if (index >= 4 && (index & 31) === 4)
601 async.invokeLater(this._setLength, this, 0);
604 Promise.prototype._clearCallbackDataAtIndex = function(index) {
606 if (!this._isCarryingStackTrace()) {
607 this._fulfillmentHandler0 = undefined;
609 this._rejectionHandler0 =
610 this._progressHandler0 =
612 this._promise0 = undefined;
614 var base = index * 5 - 5;
619 this[base + 2] = undefined;
623 Promise.prototype._isSettlePromisesQueued = function () {
624 return (this._bitField &
625 -1073741824) === -1073741824;
628 Promise.prototype._setSettlePromisesQueued = function () {
629 this._bitField = this._bitField | -1073741824;
632 Promise.prototype._unsetSettlePromisesQueued = function () {
633 this._bitField = this._bitField & (~-1073741824);
636 Promise.prototype._queueSettlePromises = function() {
637 async.settlePromises(this);
638 this._setSettlePromisesQueued();
641 Promise.prototype._fulfillUnchecked = function (value) {
642 if (value === this) {
643 var err = makeSelfResolutionError();
644 this._attachExtraTrace(err);
645 return this._rejectUnchecked(err, undefined);
647 this._setFulfilled();
648 this._settledValue = value;
651 if (this._length() > 0) {
652 this._queueSettlePromises();
656 Promise.prototype._rejectUncheckedCheckError = function (reason) {
657 var trace = util.ensureErrorObject(reason);
658 this._rejectUnchecked(reason, trace === reason ? undefined : trace);
661 Promise.prototype._rejectUnchecked = function (reason, trace) {
662 if (reason === this) {
663 var err = makeSelfResolutionError();
664 this._attachExtraTrace(err);
665 return this._rejectUnchecked(err);
668 this._settledValue = reason;
671 if (this._isFinal()) {
672 async.throwLater(function(e) {
675 CapturedTrace.unhandledRejection, undefined, e);
678 }, trace === undefined ? reason : trace);
682 if (trace !== undefined && trace !== reason) {
683 this._setCarriedStackTrace(trace);
686 if (this._length() > 0) {
687 this._queueSettlePromises();
689 this._ensurePossibleRejectionHandled();
693 Promise.prototype._settlePromises = function () {
694 this._unsetSettlePromisesQueued();
695 var len = this._length();
696 for (var i = 0; i < len; i++) {
697 this._settlePromiseAt(i);
701 util.notEnumerableProp(Promise,
702 "_makeSelfResolutionError",
703 makeSelfResolutionError);
705 require("./progress.js")(Promise, PromiseArray);
706 require("./method.js")(Promise, INTERNAL, tryConvertToPromise, apiRejection);
707 require("./bind.js")(Promise, INTERNAL, tryConvertToPromise);
708 require("./finally.js")(Promise, NEXT_FILTER, tryConvertToPromise);
709 require("./direct_resolve.js")(Promise);
710 require("./synchronous_inspection.js")(Promise);
711 require("./join.js")(Promise, PromiseArray, tryConvertToPromise, INTERNAL);
712 Promise.Promise = Promise;
713 require('./map.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL);
714 require('./cancel.js')(Promise);
715 require('./using.js')(Promise, apiRejection, tryConvertToPromise, createContext);
716 require('./generators.js')(Promise, apiRejection, INTERNAL, tryConvertToPromise);
717 require('./nodeify.js')(Promise);
718 require('./call_get.js')(Promise);
719 require('./props.js')(Promise, PromiseArray, tryConvertToPromise, apiRejection);
720 require('./race.js')(Promise, INTERNAL, tryConvertToPromise, apiRejection);
721 require('./reduce.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL);
722 require('./settle.js')(Promise, PromiseArray);
723 require('./some.js')(Promise, PromiseArray, apiRejection);
724 require('./promisify.js')(Promise, INTERNAL);
725 require('./any.js')(Promise);
726 require('./each.js')(Promise, INTERNAL);
727 require('./timers.js')(Promise, INTERNAL);
728 require('./filter.js')(Promise, INTERNAL);
730 util.toFastProperties(Promise);
731 util.toFastProperties(Promise.prototype);
732 function fillTypes(value) {
733 var p = new Promise(INTERNAL);
734 p._fulfillmentHandler0 = value;
735 p._rejectionHandler0 = value;
736 p._progressHandler0 = value;
738 p._receiver0 = value;
739 p._settledValue = value;
741 // Complete slack tracking, opt out of field-type tracking and
747 fillTypes(function(){});
748 fillTypes(undefined);
750 fillTypes(new Promise(INTERNAL));
751 CapturedTrace.setBounds(async.firstLineError, util.lastLineError);