-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromise.js
More file actions
30 lines (29 loc) · 714 Bytes
/
promise.js
File metadata and controls
30 lines (29 loc) · 714 Bytes
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
Promise.myAll = function(promiseArr) {
function done(length, resolve) {
let count = 0
let values = []
return function(value, index) {
count++
values[index] = value
if (count === length) {
resolve(values)
}
}
}
return new Promise((resolve, reject) => {
let gen = done(promiseArr.length, resolve)
promiseArr.forEach((promise, index) => {
promise.then(value => {
gen(value, index)
}, err => {
reject(err)
})
})
})
}
Promise.prototype.myfinally = function(callback) {
return this.then(
val => Promise.resolve(callback()).then(() => val),
err => Promise.resolve(callback()).then(() => { throw err })
)
}