const time = (timer) => {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, timer)
})
}
const ajax1 = () => time(2000).then(() => {
console.log(1);
return 1
})
const ajax2 = () => time(1000).then(() => {
console.log(2);
return 2
})
const ajax3 = () => time(1000).then(() => {
console.log(3);
return 3
})
function mergePromise(arr) {
let res = [];
let pro = Promise.resolve();
arr.forEach(i => {
pro = pro.then(i).then(r=>{
res.push(r);
return res;
});
});
return pro;
}
mergePromise([ajax1, ajax2, ajax3]).then(data => {
console.log('done');
console.log(data);
})