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