-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.mjs
46 lines (39 loc) · 937 Bytes
/
test.mjs
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
const timeout = ms => new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, ms);
});
const ajax1 = () => timeout(2000).then(() => {
console.log('1');
return 1;
});
const ajax2 = () => timeout(1000).then(() => {
console.log('2');
return 2;
});
const ajax3 = () => timeout(2000).then(() => {
console.log('3');
return 3;
});
const mergePromise = ajaxArray => {
// 在这里实现你的代码
return new Promise((resolve, reject) => {
let promise = Promise.resolve();
let results = [];
for (let ajax of ajaxArray) {
promise = promise.then(ajax).then(res => results.push(res));
}
promise.then(() => resolve(results), reject);
});
};
// Promise.resolve().then(ajax1).then(ajax2).then(ajax3);
mergePromise([ajax1, ajax2, ajax3]).then(data => {
console.log('done');
console.log(data); // data 为 [1, 2, 3]
});
// 要求分别输出
// 1
// 2
// 3
// done
// [1, 2, 3]