-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.js
More file actions
47 lines (43 loc) · 912 Bytes
/
1.js
File metadata and controls
47 lines (43 loc) · 912 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const time = (timer) => {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, timer)
})
}
const ajax1 = () => time(1000).then(() => {
console.log(1);
return 1
})
const ajax2 = () => time(3000).then(() => {
console.log(2);
return 2
})
const ajax3 = () => time(5000).then(() => {
console.log(3);
return 3
})
function mergePromise(arr) {
let temp = [];
return arr.reduce((p, fn, i) => {
return p.then(_ => {
return fn();
}).then(res=>{
temp[i] = result;
})
}, Promise.resolve()).then(_ => temp)
}
mergePromise([ajax1, ajax2, ajax3]).then(data => {
console.log("done");
console.log(data); // data 为 [1, 2, 3]
});
Promise.resolve().then(() => {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, 100)
}).then(() => {
console.log(2);
return 2
})
})