-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (119 loc) · 3.13 KB
/
index.js
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
'use strict';
let id = 0;
function callAnyOnce(fn1, fn2, context) {
let group
let wrapper = fn => value => {
if (!group.anyCalled) {
group.anyCalled = true
return fn.call(context, value)
}
}
group = {
fn1: wrapper(fn1),
fn2: wrapper(fn2),
anyCalled: false
}
return group
}
function getThenFromPromise(promise) {
return promise && (typeof promise === 'object' || typeof promise === 'function') && promise.then;
}
class Promise {
constructor(func) {
this.id = ++id
this.deferreds = [];
this.value = undefined;
this.onFulfill = undefined;
this.onRejected = undefined;
this.status = 'pending';
let { fn1: resolve, fn2: reject } = callAnyOnce(this._resolvePromise, this._rejectPromise, this)
func(resolve, reject)
}
then(onFulfill, onRejected) {
let deferred = Promise.deferred();
let promise = deferred.promise;
if (typeof onFulfill === 'function') {
promise.onFulfill = onFulfill;
}
if (typeof onRejected === 'function') {
promise.onRejected = onRejected;
}
this.deferreds.push(deferred);
this._triggerHandlersForCurrentStatus();
return deferred.promise;
}
_triggerHandlersForCurrentStatus() {
if (this.status === 'pending') {
return
}
setImmediate(() => {
while (this.deferreds.length) {
let deferred = this.deferreds.shift();
let handler = this.status === 'resolved' ? deferred.promise.onFulfill : deferred.promise.onRejected;
if (handler) {
try {
let result = handler(this.value);
deferred.resolve(result);
} catch (error) {
deferred.reject(error);
}
} else {
if (this.status === 'resolved') {
deferred.resolve(this.value);
} else {
deferred.reject(this.value);
}
}
}
});
}
_resolvePromise(value) {
if (value === this) {
this._rejectPromise(new TypeError('Chaining cycle detected'))
return
}
let group = callAnyOnce(this._resolvePromise, this._rejectPromise, this)
try {
let then = getThenFromPromise(value)
if (typeof then === 'function') {
then.call(value, group.fn1, group.fn2);
} else {
this.value = value;
this.status = 'resolved';
this._triggerHandlersForCurrentStatus();
}
} catch (error) {
if (this.status === 'pending' && !group.anyCalled) {
this._rejectPromise(error);
}
}
}
_rejectPromise(value) {
if (value === this) {
this._rejectPromise(new TypeError('Chaining cycle detected'))
return
}
this.value = value;
this.status = 'rejected';
this._triggerHandlersForCurrentStatus();
}
}
Promise.deferred = () => {
let def = {};
def.promise = new Promise((resolve, reject) => {
def.resolve = resolve
def.reject = reject
})
return def
};
Promise.resolve = (value) => {
let def = Promise.deferred()
def.resolve(value)
return def.promise
}
Promise.reject = (value) => {
let def = Promise.deferred()
def.reject(value)
return def.promise
}
module.exports = Promise;