-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (132 loc) · 3.54 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// import { call } from "function-bind";
// import { getHandler } from './utils';
// const PENDING = "PENDING";
// const FULFILLED = "FULFILLED";
// const REJECTED = "REJECTED";
// export default class MyPromise {
// constructor(executor) {
// this.state = PENDING;
// this.queue = [];
// this.onResolve = this.onResolve.bind(this);
// this.fulfill = this.fulfill.bind(this);
// this.reject = this.reject.bind(this);
// this.handle = this.handle.bind(this);
// this.handleResolved = this.handleResolved.bind(this);
// this.onResolve(this,executor);
// this.then = function then(onFulfilled, onRejected) {
// this.handle(this, { onFulfilled, onRejected })
// };
// }
// fulfill(promise, value) {
// promise.state = FULFILLED;
// promise.value = value;
// }
// reject(promise, reason) {
// promise.state = REJECTED;
// promise.value = reason;
// }
// handleResolved(promise, handler) {
// // if (promise.state === FULFILLED) {
// // return onFulfilled(promise.value);
// // }
// // onRejected(promise.value);
// const cb = promise.state === FULFILLED ? handler.onFulfilled : handler.onRejected
// cb(promise.value)
// if(promise.state === FULFILLED){
// return handler.onFulfilled(promise.value)
// } else {
// return handler.onRejected(promise.value)
// }
// }
// handle(promise, handler) {
// // const handlerShape = getHandler(handler)
// // const {name, method} = handlerShape
// if (promise.state === PENDING) {
// promise.queue.push(handler)
// } else {
// // execute
// promise.handleResolved(promise, handler)
// }
// }
// onResolve(promise, executor) {
// let called = false;
// function onFulfill(value) {
// if (called) return;
// called = true;
// promise.fulfill(promise, value);
// }
// function onReject(reason) {
// if (called) return;
// called = true;
// promise.reject(promise, reason);
// }
// try {
// executor(onFulfill, onReject);
// } catch (error) {
// onReject(error);
// }
// }
// }
// possible states
const PENDING = 'PENDING'
const FULFILLED = 'FULFILLED'
const REJECTED = 'REJECTED'
class MyPromise {
constructor(executor) {
this.state = PENDING
this.called = false
// value | reason = `value`
this.queue = []
// call immediately
this.doResolve(executor)
}
then(onFulfilled, onRejected){
// this.handleResolved( onFulfilled, onRejected)
this.handle({ onFulfilled, onRejected })
}
fulfill( value){
this.state = FULFILLED
this.value = value
this.finale()
}
// reject with `reason`
reject( reason) {
this.state = REJECTED
this.value = reason
this.finale()
}
handle(handler){
if(this.state === PENDING){
return this.queue.push(handler)
}
this.handleResolved(handler)
}
finale() {
const length = this.queue.length
for (let i = 0; i < length; i += 1) {
this.handle(this.queue[i])
}
}
doResolve(executor) {
const onFulfilled = (value)=>{
if(this.called) return;
this.called = true
this.fulfill(value)
}
const onRejected = (reason) => {
if(this.called) return;
this.called = true
this.reject(reason)
}
try {
executor(onFulfilled
, onRejected)
} catch (error) {
onRejected(error)
}
}
handleResolved({onFulfilled, onRejected}) {
this.state === FULFILLED ? onFulfilled(this.value) : onRejected(this.value)
}
}
module.exports = MyPromise;