Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81160 views
1
"use strict";
2
module.exports = function(Promise, INTERNAL, tryConvertToPromise) {
3
var rejectThis = function(_, e) {
4
this._reject(e);
5
};
6
7
var targetRejected = function(e, context) {
8
context.promiseRejectionQueued = true;
9
context.bindingPromise._then(rejectThis, rejectThis, null, this, e);
10
};
11
12
var bindingResolved = function(thisArg, context) {
13
this._setBoundTo(thisArg);
14
if (this._isPending()) {
15
this._resolveCallback(context.target);
16
}
17
};
18
19
var bindingRejected = function(e, context) {
20
if (!context.promiseRejectionQueued) this._reject(e);
21
};
22
23
Promise.prototype.bind = function (thisArg) {
24
var maybePromise = tryConvertToPromise(thisArg);
25
var ret = new Promise(INTERNAL);
26
ret._propagateFrom(this, 1);
27
var target = this._target();
28
if (maybePromise instanceof Promise) {
29
var context = {
30
promiseRejectionQueued: false,
31
promise: ret,
32
target: target,
33
bindingPromise: maybePromise
34
};
35
target._then(INTERNAL, targetRejected, ret._progress, ret, context);
36
maybePromise._then(
37
bindingResolved, bindingRejected, ret._progress, ret, context);
38
} else {
39
ret._setBoundTo(thisArg);
40
ret._resolveCallback(target);
41
}
42
return ret;
43
};
44
45
Promise.prototype._setBoundTo = function (obj) {
46
if (obj !== undefined) {
47
this._bitField = this._bitField | 131072;
48
this._boundTo = obj;
49
} else {
50
this._bitField = this._bitField & (~131072);
51
}
52
};
53
54
Promise.prototype._isBound = function () {
55
return (this._bitField & 131072) === 131072;
56
};
57
58
Promise.bind = function (thisArg, value) {
59
var maybePromise = tryConvertToPromise(thisArg);
60
var ret = new Promise(INTERNAL);
61
62
if (maybePromise instanceof Promise) {
63
maybePromise._then(function(thisArg) {
64
ret._setBoundTo(thisArg);
65
ret._resolveCallback(value);
66
}, ret._reject, ret._progress, ret, null);
67
} else {
68
ret._setBoundTo(thisArg);
69
ret._resolveCallback(value);
70
}
71
return ret;
72
};
73
};
74
75