-
Notifications
You must be signed in to change notification settings - Fork 1
/
fakePromiseAll.js
73 lines (55 loc) · 1.24 KB
/
fakePromiseAll.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
const fakePromiseAll = (promises) => {
const len = promises.length
let count = 0
let result = []
return new Promise((resolve, reject) => {
promises.forEach(promise => {
promise.then(rs => {
count++
result = result.concat(rs)
if (count === len) {
resolve(result)
}
}, err => {
reject([err])
})
})
})
}
const fakeAsync = (timer, type) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(`${timer}---${type}`)
if (timer === 1000) {
reject(`${timer}---${type}`)
} else {
resolve(`${timer}---${type}`)
}
}, timer)
})
}
const arr = [
fakeAsync(500, 'first'),
fakeAsync(1000, 'second'),
fakeAsync(2000, 'third'),
fakeAsync(2000, 'fourth'),
fakeAsync(1000, 'five'),
fakeAsync(1000, 'five1'),
fakeAsync(3000, 'five2'),
fakeAsync(3000, 'five3'),
]
// fakePromiseAll
fakePromiseAll(arr).then(rs => {
console.log('---', rs)
}, er => {
console.log('----err', er)
})
// const f = () => 1;
// (async () => f())().then(rs => {
// console.log('sucess', rs)
// }, err => {
// console.log('err', err)
// });
// Promise.try(() => fakeAsync(500)).then(rs => {
// console.log('rs', rs)
// })