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
|
"use strict";
module.exports = function(Promise, INTERNAL, tryConvertToPromise) {
var rejectThis = function(_, e) {
this._reject(e);
};
var targetRejected = function(e, context) {
context.promiseRejectionQueued = true;
context.bindingPromise._then(rejectThis, rejectThis, null, this, e);
};
var bindingResolved = function(thisArg, context) {
this._setBoundTo(thisArg);
if (this._isPending()) {
this._resolveCallback(context.target);
}
};
var bindingRejected = function(e, context) {
if (!context.promiseRejectionQueued) this._reject(e);
};
Promise.prototype.bind = function (thisArg) {
var maybePromise = tryConvertToPromise(thisArg);
var ret = new Promise(INTERNAL);
ret._propagateFrom(this, 1);
var target = this._target();
if (maybePromise instanceof Promise) {
var context = {
promiseRejectionQueued: false,
promise: ret,
target: target,
bindingPromise: maybePromise
};
target._then(INTERNAL, targetRejected, ret._progress, ret, context);
maybePromise._then(
bindingResolved, bindingRejected, ret._progress, ret, context);
} else {
ret._setBoundTo(thisArg);
ret._resolveCallback(target);
}
return ret;
};
Promise.prototype._setBoundTo = function (obj) {
if (obj !== undefined) {
this._bitField = this._bitField | 131072;
this._boundTo = obj;
} else {
this._bitField = this._bitField & (~131072);
}
};
Promise.prototype._isBound = function () {
return (this._bitField & 131072) === 131072;
};
Promise.bind = function (thisArg, value) {
var maybePromise = tryConvertToPromise(thisArg);
var ret = new Promise(INTERNAL);
if (maybePromise instanceof Promise) {
maybePromise._then(function(thisArg) {
ret._setBoundTo(thisArg);
ret._resolveCallback(value);
}, ret._reject, ret._progress, ret, null);
} else {
ret._setBoundTo(thisArg);
ret._resolveCallback(value);
}
return ret;
};
};
|