| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816 |
8
8
44
8
24
8
82
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
36863
5
36858
1
36855
36855
36855
36855
36855
36855
36855
36855
36855
8
45
8
947
947
412
412
430
430
428
2
410
410
410
410
535
8
24
8
12775
8
25
25
8
113
113
113
8
318
8
12
12
3
3
9
3
3
12
8
110
110
110
8
48
8
6
8
103
103
103
8
2495
2495
8
5881
5881
5526
5526
5526
5881
8
8
1661
1661
1661
1661
8
2
1
8
18972
18972
18972
18972
17834
17834
18972
18972
171
170
170
137
137
18972
18972
9261
18972
8
9261
9261
9261
9261
9261
9261
8
81484
81484
8
111309
8
395908
8
31130
8
27815
8
5869
8
2913
8
25
8
5869
8
55692
8
360
8
75
8
113
8
844
8
2481
8
1310
8
19031
8
28318
28318
28318
219
28099
8
39000
39000
8
23179
23179
23179
8
7202
7202
8
2349
2349
2349
2349
2349
2349
2311
2311
1132
1132
2349
8
21321
21321
21321
21321
19266
19266
19266
19266
19266
19266
19266
19266
14348
19266
19266
2055
2055
2055
2055
2055
2055
2055
2055
2055
1958
2055
1729
2055
1566
21321
21321
8
6830
6830
6830
6830
6794
6794
36
36
36
6830
8
6830
6830
6830
6830
6830
6830
8
23303
4539
18764
8
4380
2443
4380
4380
4380
4380
8
2146
2146
2146
2146
2146
2146
1985
1979
1979
93
84
84
2146
2146
2146
35
35
8
16947
16944
16944
16944
16944
92
16852
16944
16944
873
873
16071
8
34339
34339
34339
8
722
722
722
8
2913
2913
2913
8
4899
4899
4899
4899
4899
2913
2913
2349
2913
2913
2913
2913
1986
1031
955
4899
8
23312
23312
48
23264
23264
18722
4542
4542
8
33684
33684
195
8
21466
234
234
21466
18283
21466
8
21366
21252
8
4971
4896
8
26993
26993
26993
26993
1310
1310
25683
25683
25683
25683
25683
25683
25683
1217
1217
25683
25683
18082
1135
16947
7601
6827
6752
6676
76
774
774
299
475
25683
66
8
25683
25683
23598
20194
23598
2085
2085
8
25417
8
16125
8
16125
8
16125
16125
16125
16125
8
27821
27821
6
6
6
27815
27815
27815
27815
13169
8
12
12
8
5875
5875
6
6
6
5869
5869
5869
5869
3
3
3
3
3
3
5866
4358
5866
2956
2910
8
16125
16125
16125
16422
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
| (function () { "use strict";
module.exports = function() {
var makeSelfResolutionError = function () {
return new TypeError("circular promise resolution chain\u000a\u000a See http://goo.gl/LhFpo0\u000a");
};
var reflect = function() {
return new Promise.PromiseInspection(this._target());
};
var apiRejection = function(msg) {
return Promise.reject(new TypeError(msg));
};
var ASSERT = require("./assert.js");
var util = require("./util.js");
var async = require("./async.js");
var errors = require("./errors.js");
var TypeError = Promise.TypeError = errors.TypeError;
Promise.RangeError = errors.RangeError;
Promise.CancellationError = errors.CancellationError;
Promise.TimeoutError = errors.TimeoutError;
Promise.OperationalError = errors.OperationalError;
Promise.RejectionError = errors.OperationalError;
Promise.AggregateError = errors.AggregateError;
var INTERNAL = function(){};
var APPLY = {};
var NEXT_FILTER = {e: null};
var tryConvertToPromise = require("./thenables.js")(Promise, INTERNAL);
var PromiseArray =
require("./promise_array.js")(Promise, INTERNAL,
tryConvertToPromise, apiRejection);
var CapturedTrace = require("./captured_trace.js")();
var isDebugging = require("./debuggability.js")(Promise, CapturedTrace);
/*jshint unused:false*/
var createContext =
require("./context.js")(Promise, CapturedTrace, isDebugging);
var CatchFilter = require("./catch_filter.js")(NEXT_FILTER);
var PromiseResolver = require("./promise_resolver.js");
var errorObj = util.errorObj;
var tryCatch = util.tryCatch;
function Promise(resolver) {
if (typeof resolver !== "function") {
throw new TypeError("the promise constructor requires a resolver function\u000a\u000a See http://goo.gl/EC22Yn\u000a");
}
if (this.constructor !== Promise) {
throw new TypeError("the promise constructor cannot be invoked directly\u000a\u000a See http://goo.gl/KsIlge\u000a");
}
this._bitField = 0;
this._fulfillmentHandler0 = undefined;
this._rejectionHandler0 = undefined;
this._progressHandler0 = undefined;
this._promise0 = undefined;
this._receiver0 = undefined;
this._settledValue = undefined;
this._boundTo = undefined;
if (resolver !== INTERNAL) this._resolveFromResolver(resolver);
}
Promise.prototype.toString = function () {
return "[object Promise]";
};
Promise.prototype.caught = Promise.prototype["catch"] = function (fn) {
var len = arguments.length;
if (len > 1) {
var catchInstances = new Array(len - 1),
j = 0, i;
for (i = 0; i < len - 1; ++i) {
var item = arguments[i];
if (typeof item === "function") {
catchInstances[j++] = item;
} else {
return Promise.reject(
new TypeError("Catch filter must inherit from Error or be a simple predicate function\u000a\u000a See http://goo.gl/o84o68\u000a"));
}
}
catchInstances.length = j;
fn = arguments[i];
var catchFilter = new CatchFilter(catchInstances, fn, this);
return this._then(undefined, catchFilter.doFilter, undefined,
catchFilter, undefined);
}
return this._then(undefined, fn, undefined, undefined, undefined);
};
Promise.prototype.reflect = function () {
return this._then(reflect, reflect, undefined, this, undefined);
};
Promise.prototype.then = function (didFulfill, didReject, didProgress) {
return this._then(didFulfill, didReject, didProgress,
undefined, undefined);
};
Promise.prototype.done = function (didFulfill, didReject, didProgress) {
var promise = this._then(didFulfill, didReject, didProgress,
undefined, undefined);
promise._setIsFinal();
};
Promise.prototype.spread = function (didFulfill, didReject) {
var followee = this._target();
var target = followee._isSpreadable()
? (followee === this ? this : this.then())
: this.all();
return target._then(didFulfill, didReject, undefined, APPLY, undefined);
};
Promise.prototype.isCancellable = function () {
return !this.isResolved() &&
this._cancellable();
};
Promise.prototype.toJSON = function () {
var ret = {
isFulfilled: false,
isRejected: false,
fulfillmentValue: undefined,
rejectionReason: undefined
};
if (this.isFulfilled()) {
ret.fulfillmentValue = this.value();
ret.isFulfilled = true;
} else if (this.isRejected()) {
ret.rejectionReason = this.reason();
ret.isRejected = true;
}
return ret;
};
Promise.prototype.all = function () {
var ret = new PromiseArray(this).promise();
ret._setIsSpreadable();
return ret;
};
Promise.prototype.error = function (fn) {
return this.caught(util.originatesFromRejection, fn);
};
Promise.is = function (val) {
return val instanceof Promise;
};
Promise.all = function (promises) {
var ret = new PromiseArray(promises).promise();
ret._setIsSpreadable();
return ret;
};
Promise.defer = Promise.pending = function () {
var promise = new Promise(INTERNAL);
return new PromiseResolver(promise);
};
Promise.cast = function (obj) {
var ret = tryConvertToPromise(obj);
if (!(ret instanceof Promise)) {
var val = ret;
ret = new Promise(INTERNAL);
ret._fulfillUnchecked(val);
}
return ret;
};
Promise.resolve = Promise.fulfilled = Promise.cast;
Promise.reject = Promise.rejected = function (reason) {
var ret = new Promise(INTERNAL);
ret._captureStackTrace();
ret._rejectCallback(reason, true);
return ret;
};
Promise.setScheduler = function(fn) {
if (typeof fn !== "function") throw new TypeError("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
async._schedule = fn;
};
Promise.prototype._then = function (
didFulfill,
didReject,
didProgress,
receiver,
internalData
) {
ASSERT((arguments.length === 5),
"arguments.length === 5");
var haveInternalData = internalData !== undefined;
var ret = haveInternalData ? internalData : new Promise(INTERNAL);
if (!haveInternalData) {
ret._propagateFrom(this, 4 | 1);
ret._captureStackTrace();
}
var target = this._target();
if (target !== this) {
if (!haveInternalData) {
ret._setIsMigrated();
if (receiver === undefined) {
ret._setIsMigratingBinding();
receiver = this;
}
}
}
var callbackIndex =
target._addCallbacks(didFulfill, didReject, didProgress, ret, receiver);
if (target._isResolved() && !target._isSettlePromisesQueued()) {
async.invoke(
target._settlePromiseAtPostResolution, target, callbackIndex);
}
return ret;
};
Promise.prototype._settlePromiseAtPostResolution = function (index) {
ASSERT(((typeof index) === "number"),
"typeof index === \u0022number\u0022");
ASSERT((index >= 0),
"index >= 0");
ASSERT((this._isFulfilled() || this._isRejected()),
"this._isFulfilled() || this._isRejected()");
ASSERT((this._promiseAt(index) !== undefined),
"this._promiseAt(index) !== undefined");
if (this._isRejectionUnhandled()) this._unsetRejectionIsUnhandled();
this._settlePromiseAt(index);
};
Promise.prototype._length = function () {
ASSERT((arguments.length === 0),
"arguments.length === 0");
return this._bitField & 131071;
};
Promise.prototype._isFollowingOrFulfilledOrRejected = function () {
return (this._bitField & 939524096) > 0;
};
Promise.prototype._isFollowing = function () {
return (this._bitField & 536870912) === 536870912;
};
Promise.prototype._setLength = function (len) {
this._bitField = (this._bitField & -131072) |
(len & 131071);
};
Promise.prototype._setFulfilled = function () {
this._bitField = this._bitField | 268435456;
};
Promise.prototype._setRejected = function () {
this._bitField = this._bitField | 134217728;
};
Promise.prototype._setFollowing = function () {
this._bitField = this._bitField | 536870912;
};
Promise.prototype._setIsFinal = function () {
this._bitField = this._bitField | 33554432;
};
Promise.prototype._isFinal = function () {
return (this._bitField & 33554432) > 0;
};
Promise.prototype._cancellable = function () {
return (this._bitField & 67108864) > 0;
};
Promise.prototype._setCancellable = function () {
this._bitField = this._bitField | 67108864;
};
Promise.prototype._unsetCancellable = function () {
this._bitField = this._bitField & (~67108864);
};
Promise.prototype._isSpreadable = function () {
return (this._bitField & 131072) > 0;
};
Promise.prototype._setIsSpreadable = function () {
this._bitField = this._bitField | 131072;
};
Promise.prototype._setIsMigrated = function () {
this._bitField = this._bitField | 4194304;
};
Promise.prototype._unsetIsMigrated = function () {
this._bitField = this._bitField & (~4194304);
};
Promise.prototype._isMigrated = function () {
return (this._bitField & 4194304) > 0;
};
Promise.prototype._receiverAt = function (index) {
ASSERT((! this._isFollowing()),
"!this._isFollowing()");
var ret = index === 0
? this._receiver0
: this[
index * 5 - 5 + 4];
if (this._isBound() && ret === undefined) {
return this._boundTo;
}
return ret;
};
Promise.prototype._promiseAt = function (index) {
ASSERT((! this._isFollowing()),
"!this._isFollowing()");
return index === 0
? this._promise0
: this[index * 5 - 5 + 3];
};
Promise.prototype._fulfillmentHandlerAt = function (index) {
ASSERT((! this._isFollowing()),
"!this._isFollowing()");
ASSERT((! this._isCarryingStackTrace()),
"!this._isCarryingStackTrace()");
return index === 0
? this._fulfillmentHandler0
: this[index * 5 - 5 + 0];
};
Promise.prototype._rejectionHandlerAt = function (index) {
ASSERT((! this._isFollowing()),
"!this._isFollowing()");
return index === 0
? this._rejectionHandler0
: this[index * 5 - 5 + 1];
};
Promise.prototype._migrateCallbacks = function (follower, index) {
var fulfill = follower._fulfillmentHandlerAt(index);
var reject = follower._rejectionHandlerAt(index);
var progress = follower._progressHandlerAt(index);
var promise = follower._promiseAt(index);
var receiver = follower._receiverAt(index);
if (promise instanceof Promise) {
promise._setIsMigrated();
if (receiver === undefined) {
receiver = follower;
promise._setIsMigratingBinding();
}
}
this._addCallbacks(fulfill, reject, progress, promise, receiver);
};
Promise.prototype._addCallbacks = function (
fulfill,
reject,
progress,
promise,
receiver
) {
ASSERT((! this._isFollowing()),
"!this._isFollowing()");
var index = this._length();
Iif (index >= 131071 - 5) {
index = 0;
this._setLength(0);
}
if (index === 0) {
ASSERT((this._promise0 === undefined),
"this._promise0 === undefined");
ASSERT((this._receiver0 === undefined),
"this._receiver0 === undefined");
ASSERT(((this._fulfillmentHandler0 === undefined) || this._isCarryingStackTrace()),
"this._fulfillmentHandler0 === undefined ||\u000a this._isCarryingStackTrace()");
ASSERT((this._rejectionHandler0 === undefined),
"this._rejectionHandler0 === undefined");
ASSERT((this._progressHandler0 === undefined),
"this._progressHandler0 === undefined");
this._promise0 = promise;
if (receiver !== undefined) this._receiver0 = receiver;
if (typeof fulfill === "function" && !this._isCarryingStackTrace())
this._fulfillmentHandler0 = fulfill;
if (typeof reject === "function") this._rejectionHandler0 = reject;
if (typeof progress === "function") this._progressHandler0 = progress;
} else {
ASSERT((this[(base + 3)] === undefined),
"this[base + CALLBACK_PROMISE_OFFSET] === undefined");
ASSERT((this[(base + 4)] === undefined),
"this[base + CALLBACK_RECEIVER_OFFSET] === undefined");
ASSERT((this[(base + 0)] === undefined),
"this[base + CALLBACK_FULFILL_OFFSET] === undefined");
ASSERT((this[(base + 1)] === undefined),
"this[base + CALLBACK_REJECT_OFFSET] === undefined");
ASSERT((this[(base + 2)] === undefined),
"this[base + CALLBACK_PROGRESS_OFFSET] === undefined");
var base = index * 5 - 5;
this[base + 3] = promise;
this[base + 4] = receiver;
if (typeof fulfill === "function")
this[base + 0] = fulfill;
if (typeof reject === "function")
this[base + 1] = reject;
if (typeof progress === "function")
this[base + 2] = progress;
}
this._setLength(index + 1);
return index;
};
Promise.prototype._setProxyHandlers = function (receiver, promiseSlotValue) {
ASSERT((! this._isFollowing()),
"!this._isFollowing()");
var index = this._length();
Iif (index >= 131071 - 5) {
index = 0;
this._setLength(0);
}
if (index === 0) {
this._promise0 = promiseSlotValue;
this._receiver0 = receiver;
} else {
var base = index * 5 - 5;
this[base + 3] = promiseSlotValue;
this[base + 4] = receiver;
}
this._setLength(index + 1);
};
Promise.prototype._proxyPromiseArray = function (promiseArray, index) {
ASSERT((! this._isFollowing()),
"!this._isFollowing()");
ASSERT((! this._isResolved()),
"!this._isResolved()");
ASSERT((arguments.length === 2),
"arguments.length === 2");
ASSERT(((typeof index) === "number"),
"typeof index === \u0022number\u0022");
ASSERT(((index | 0) === index),
"(index | 0) === index");
this._setProxyHandlers(promiseArray, index);
};
Promise.prototype._resolveCallback = function(value) {
if (this._tryFollow(value)) {
return;
}
this._fulfill(value);
};
Promise.prototype._rejectCallback =
function(reason, synchronous, shouldNotMarkOriginatingFromRejection) {
if (!shouldNotMarkOriginatingFromRejection) {
util.markAsOriginatingFromRejection(reason);
}
var trace = util.ensureErrorObject(reason);
var hasStack = util.canAttachTrace(reason) &&
typeof trace.stack === "string";
this._attachExtraTrace(trace, synchronous ? hasStack : false);
this._reject(reason, trace === reason ? undefined : trace);
};
Promise.prototype._resolveFromResolver = function (resolver) {
ASSERT(((typeof resolver) === "function"),
"typeof resolver === \u0022function\u0022");
var promise = this;
this._captureStackTrace();
this._pushContext();
var synchronous = true;
var r = tryCatch(resolver)(function(value) {
if (promise === null) return;
promise._resolveCallback(value);
promise = null;
}, function (reason) {
if (promise === null) return;
promise._rejectCallback(reason, synchronous);
promise = null;
});
synchronous = false;
this._popContext();
if (r !== undefined && r === errorObj && promise !== null) {
promise._rejectCallback(r.e, true, true);
promise = null;
}
};
Promise.prototype._settlePromiseFromHandler = function (
handler, receiver, value, promise
) {
if (promise._isRejected()) return;
ASSERT((! promise._isFollowingOrFulfilledOrRejected()),
"!promise._isFollowingOrFulfilledOrRejected()");
promise._pushContext();
var x;
if (receiver === APPLY && !this._isRejected()) {
x = tryCatch(handler).apply(this._boundTo, value);
} else {
x = tryCatch(handler).call(receiver, value);
}
promise._popContext();
if (x === errorObj || x === promise || x === NEXT_FILTER) {
var err = x === promise ? makeSelfResolutionError() : x.e;
promise._rejectCallback(err, false, true);
} else {
promise._resolveCallback(x);
}
};
Promise.prototype._target = function() {
var ret = this;
while (ret._isFollowing()) ret = ret._followee();
return ret;
};
Promise.prototype._followee = function() {
ASSERT(this._isFollowing(),
"this._isFollowing()");
ASSERT((this._rejectionHandler0 instanceof Promise),
"this._rejectionHandler0 instanceof Promise");
return this._rejectionHandler0;
};
Promise.prototype._setFollowee = function(promise) {
ASSERT(this._isFollowing(),
"this._isFollowing()");
ASSERT((! (this._rejectionHandler0 instanceof Promise)),
"!(this._rejectionHandler0 instanceof Promise)");
this._rejectionHandler0 = promise;
};
Promise.prototype._follow = function (promise) {
ASSERT((arguments.length === 1),
"arguments.length === 1");
ASSERT((! this._isFollowingOrFulfilledOrRejected()),
"!this._isFollowingOrFulfilledOrRejected()");
ASSERT((! promise._isFollowing()),
"!promise._isFollowing()");
ASSERT((promise !== this),
"promise !== this");
if (promise._isPending()) {
var len = this._length();
for (var i = 0; i < len; ++i) {
promise._migrateCallbacks(this, i);
}
this._setFollowing();
this._setLength(0);
this._setFollowee(promise);
this._propagateFrom(promise, 1);
} else if (promise._isFulfilled()) {
this._fulfillUnchecked(promise._value());
} else {
this._rejectUnchecked(promise._reason(),
promise._getCarriedStackTrace());
}
ASSERT(this._isFollowingOrFulfilledOrRejected(),
"this._isFollowingOrFulfilledOrRejected()");
};
Promise.prototype._tryFollow = function (value) {
ASSERT((arguments.length === 1),
"arguments.length === 1");
if (this._isFollowingOrFulfilledOrRejected() ||
value === this) {
return false;
}
var maybePromise = tryConvertToPromise(value, this);
if (!(maybePromise instanceof Promise)) {
return false;
}
this._follow(maybePromise._target());
return true;
};
Promise.prototype._cleanValues = function () {
ASSERT((! this._isFollowing()),
"!this._isFollowing()");
if (this._cancellable()) {
this._cancellationParent = undefined;
}
};
Promise.prototype._propagateFrom = function (parent, flags) {
if ((flags & 1) > 0 && parent._cancellable()) {
this._setCancellable();
this._cancellationParent = parent;
}
if ((flags & 4) > 0) {
this._setBoundTo(parent._boundTo);
}
ASSERT(((flags & 2) === 0),
"(flags & PROPAGATE_TRACE) === 0");
};
Promise.prototype._fulfill = function (value) {
if (this._isFollowingOrFulfilledOrRejected()) return;
this._fulfillUnchecked(value);
};
Promise.prototype._reject = function (reason, carriedStackTrace) {
if (this._isFollowingOrFulfilledOrRejected()) return;
this._rejectUnchecked(reason, carriedStackTrace);
};
Promise.prototype._settlePromiseAt = function (index) {
var promise = this._promiseAt(index);
var isPromise = promise instanceof Promise;
ASSERT(async._isTickUsed,
"async._isTickUsed");
if (isPromise && promise._isMigrated()) {
promise._unsetIsMigrated();
return async.invoke(this._settlePromiseAt, this, index);
}
ASSERT((! this._isFollowing()),
"!this._isFollowing()");
var handler = this._isFulfilled()
? this._fulfillmentHandlerAt(index)
: this._rejectionHandlerAt(index);
ASSERT((this._isFulfilled() || this._isRejected()),
"this._isFulfilled() || this._isRejected()");
var carriedStackTrace =
this._isCarryingStackTrace() ? this._getCarriedStackTrace() : undefined;
var value = this._settledValue;
var receiver = this._receiverAt(index);
if (isPromise && promise._isMigratingBinding()) {
promise._unsetIsMigratingBinding();
receiver = receiver._boundTo;
}
this._clearCallbackDataAtIndex(index);
if (typeof handler === "function") {
if (!isPromise) {
handler.call(receiver, value, promise);
} else {
this._settlePromiseFromHandler(handler, receiver, value, promise);
}
} else if (receiver instanceof PromiseArray) {
if (!receiver._isResolved()) {
if (this._isFulfilled()) {
receiver._promiseFulfilled(value, promise);
}
else {
receiver._promiseRejected(value, promise);
}
}
} else Eif (isPromise) {
if (this._isFulfilled()) {
promise._fulfill(value);
} else {
promise._reject(value, carriedStackTrace);
}
}
if (index >= 4 && (index & 31) === 4)
async.invokeLater(this._setLength, this, 0);
};
Promise.prototype._clearCallbackDataAtIndex = function(index) {
ASSERT((! this._isFollowing()),
"!this._isFollowing()");
if (index === 0) {
if (!this._isCarryingStackTrace()) {
this._fulfillmentHandler0 = undefined;
}
this._rejectionHandler0 =
this._progressHandler0 =
this._receiver0 =
this._promise0 = undefined;
} else {
var base = index * 5 - 5;
this[base + 3] =
this[base + 4] =
this[base + 0] =
this[base + 1] =
this[base + 2] = undefined;
}
};
Promise.prototype._isSettlePromisesQueued = function () {
return (this._bitField &
-1073741824) === -1073741824;
};
Promise.prototype._setSettlePromisesQueued = function () {
this._bitField = this._bitField | -1073741824;
};
Promise.prototype._unsetSettlePromisesQueued = function () {
this._bitField = this._bitField & (~-1073741824);
};
Promise.prototype._queueSettlePromises = function() {
ASSERT((! this._isFollowing()),
"!this._isFollowing()");
ASSERT((! this._isSettlePromisesQueued()),
"!this._isSettlePromisesQueued()");
async.settlePromises(this);
this._setSettlePromisesQueued();
};
Promise.prototype._fulfillUnchecked = function (value) {
ASSERT((! this._isFollowingOrFulfilledOrRejected()),
"!this._isFollowingOrFulfilledOrRejected()");
if (value === this) {
var err = makeSelfResolutionError();
this._attachExtraTrace(err);
return this._rejectUnchecked(err, undefined);
}
this._setFulfilled();
this._settledValue = value;
this._cleanValues();
if (this._length() > 0) {
this._queueSettlePromises();
}
};
Promise.prototype._rejectUncheckedCheckError = function (reason) {
var trace = util.ensureErrorObject(reason);
this._rejectUnchecked(reason, trace === reason ? undefined : trace);
};
Promise.prototype._rejectUnchecked = function (reason, trace) {
ASSERT((! this._isFollowingOrFulfilledOrRejected()),
"!this._isFollowingOrFulfilledOrRejected()");
if (reason === this) {
var err = makeSelfResolutionError();
this._attachExtraTrace(err);
return this._rejectUnchecked(err);
}
this._setRejected();
this._settledValue = reason;
this._cleanValues();
if (this._isFinal()) {
ASSERT((this._length() === 0),
"this._length() === 0");
async.throwLater(function(e) {
Eif ("stack" in e) {
async.invokeFirst(
CapturedTrace.unhandledRejection, undefined, e);
}
throw e;
}, trace === undefined ? reason : trace);
return;
}
if (trace !== undefined && trace !== reason) {
this._setCarriedStackTrace(trace);
}
if (this._length() > 0) {
this._queueSettlePromises();
} else {
this._ensurePossibleRejectionHandled();
}
};
Promise.prototype._settlePromises = function () {
this._unsetSettlePromisesQueued();
var len = this._length();
for (var i = 0; i < len; i++) {
this._settlePromiseAt(i);
}
};
Promise._makeSelfResolutionError = makeSelfResolutionError;
require("./method.js")(Promise, INTERNAL, tryConvertToPromise, apiRejection);
require("./bind.js")(Promise, INTERNAL, tryConvertToPromise);
require("./finally.js")(Promise, NEXT_FILTER, tryConvertToPromise);
require("./direct_resolve.js")(Promise);
require("./synchronous_inspection.js")(Promise);
require("./join.js")(Promise, PromiseArray, tryConvertToPromise, INTERNAL);
util.toFastProperties(Promise);
util.toFastProperties(Promise.prototype);
Promise.Promise = Promise;
CapturedTrace.setBounds(async.firstLineError, util.lastLineError);
require('./map.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL);
require('./using.js')(Promise, apiRejection, tryConvertToPromise, createContext);
require('./generators.js')(Promise, apiRejection, INTERNAL, tryConvertToPromise);
require('./nodeify.js')(Promise);
require('./cancel.js')(Promise, INTERNAL);
require('./promisify.js')(Promise, INTERNAL);
require('./props.js')(Promise, PromiseArray, tryConvertToPromise, apiRejection);
require('./race.js')(Promise, INTERNAL, tryConvertToPromise, apiRejection);
require('./reduce.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL);
require('./settle.js')(Promise, PromiseArray);
require('./call_get.js')(Promise);
require('./some.js')(Promise, PromiseArray, apiRejection);
require('./progress.js')(Promise, PromiseArray);
require('./any.js')(Promise);
require('./each.js')(Promise, INTERNAL);
require('./timers.js')(Promise, INTERNAL, tryConvertToPromise);
require('./filter.js')(Promise, INTERNAL);
Promise.prototype = Promise.prototype;
return Promise;
};
}());
|