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