-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path实现一个简单的promise.js
206 lines (189 loc) · 4.66 KB
/
实现一个简单的promise.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// function ccPromise(cb) {
// var that = this
// that.onResolvedCallback = []
// function resolve(value) {
// setTimeout(() => {
// that.date = value
// that.onResolvedCallback.forEach(callback => callback(value))
// })
// }
// cb(resolve.bind(that))
// }
// ccPromise.prototype.then = function(onResolved) {
// var that = this
// return new ccPromise(resolve => {
// that.onResolvedCallback.push(function() {
// var result = onResolved(that.data)
// if(result instanceof ccPromise) {
// result.then(resolve)
// } else {
// resolve(result)
// }
// })
// })
// }
// new ccPromise(resolve => {
// setTimeout(() => {
// resolve(1)
// }, 500)
// })
// .then(res => {
// console.log(res)
// return new ccPromise(resolve => {
// setTimeout(() => {
// resolve(2)
// }, 500)
// })
// })
// .then(console.log)
//极简型
/**
* excutor 执行器函数(同步)
*/
!function () {
function Promise(excutor) {
this.status = 'pendding'
this.data = undefined
this.callbacks = [] //{onResloved:() => {}, onRejected:() => {}}
// let _this = this
const resolve = (value) => {
if (this.status !== 'pendding') return
this.status = 'resolved'
this.data = value
if (this.callbacks.length > 0) {
setTimeout(() => {
this.callbacks.forEach(cbObj => {
cbObj['onResolved'](value)
})
}, 0)
}
}
const reject = (reason) => {
if (this.status !== 'pendding') return
this.status = 'rejected'
this.data = reason
if (this.callbacks.length > 0) {
setTimeout(() => {
this.callbacks.forEach(cbObj => {
cbObj['onRejected'](value)
})
}, 0)
}
}
try {
excutor(resolve, reject)
} catch (err) {
reject(err)
}
}
Promise.prototype.then = function (onResolved, onRejected) {
onResolved = typeof onResolved === 'function' ? onResolved : value => value
onRejected = typeof onRejected === 'function' ? onRejected : reason => {throw reason}
return new Promise((resolve, reject) => {
const handle = (callback) => {
try {
const res = callback(this.data)
if (res instanceof Promise) {
// res.then(value => resolve(value), err => reject(err))
res.then(resolve, reject)
} else {
resolve(res)
}
} catch (error) {
reject(error)
}
}
// console.log(this.status)
if (this.status === 'pendding') {
this.callbacks.push({
onResolved: (value) => {
// onResolved(this.data)
// onResolved(value)
handle(onResolved)
},
onRejected: (reason) => {
// onRejected(reason)
handle(onRejected)
}
})
} else if (this.status === 'resolved') {
setTimeout(() => {
handle(onResolved)
// resolve(this.data)
})
} else {
setTimeout(() => {
handle(onRejected)
});
}
})
}
Promise.prototype.catch = function (onRejected) {
return this.then(undefined, onRejected)
}
Promise.resolve = function (value) {
return new Promise(resolve => {
resolve()
})
}
Promise.rejected = function (reason) {
return new Promise((_, reject) => {
reject()
})
}
Promise.all = function (promises) {
let len = promises.length, index = 0, results = []
return new Promise((resolve, reject) => {
for(let i = 0; i < len; i++) {
let item = promises[i]
if(item instanceof Promise) {
item.then(res => {
results[i] = res
index++
if(index === len) {
resolve(results)
}
}).catch(reason => {
reject(reason)
})
} else {
results[i] = item
index++
if(index === len) {
resolve(results)
}
}
}
})
}
Promise.race = function (promises) {
}
global.Promise = Promise
}(global)
function red() {
console.log('red')
}
function green() {
console.log('green')
}
function yellow() {
console.log('yellow')
}
function light(cb, timer) {
return new Promise((resolve, reject) => {
setTimeout(() => {
cb()
resolve()
}, timer);
})
}
function change() {
light(red, 3000).
then(() => {
return light(green, 2000)
}).then(() => {
return light(yellow, 2000)
}).then(() => {
change()
})
}