实现mergePromise函数,把传进去的数组按顺序先后执行,并且把返回的数据先后放到数组data中。
1const time = (timer) => { 2 return new Promise(resolve => { 3 setTimeout(() => { 4 resolve() 5 }, timer) 6 }) 7} 8const ajax1 = () => time(2000).then(() => { 9 console.log(1); 10 return 1 11}) 12const ajax2 = () => time(1000).then(() => { 13 console.log(2); 14 return 2 15}) 16const ajax3 = () => time(1000).then(() => { 17 console.log(3); 18 return 3 19}) 20 21function mergePromise () { 22 // 在这里写代码 23} 24 25mergePromise([ajax1, ajax2, ajax3]).then(data => { 26 console.log("done"); 27 console.log(data); // data 为 [1, 2, 3] 28}); 29 30// 要求分别输出 31// 1 32// 2 33// 3 34// done 35// [1, 2, 3] 36