4 回答
TA贡献1806条经验 获得超5个赞
f1().then(data1 => promise.all([f2(data1), f3(data1)]))
.then(([data2, data3]) => promsie.all([f4({ data2, data3 }), f5()])
.then(([data4, data5]) => f6({ data3, data4, data5 })))
.then(data6 => console.log('结束'))
TA贡献1789条经验 获得超10个赞
let temp = f1().then(data1 => {
return Promise.all([f2(), f3()])
}).then(([data2, data3]) => {
return Promise.all([f4(), Promise.resolve(data3)])
})
Promise.all([temp, f5()]).then(([[data4, data3], data5]) => {
console.log(data4, data3, data5)
return f6()
}).catch(error=>{
console.log(error)
})
TA贡献1757条经验 获得超7个赞
f5没有依赖,所以f5执行的越早越有利,尤其是f5响应时间>f4的时候
// 模拟下请求
const timeConsumingFunc = param=>new Promise(
(resolve) => {
let timeout = Math.random() * 5000;
console.log(`task ${param} will be resolved in ${timeout}ms`);
setTimeout(() => {
console.log(`${param} resolved`);
resolve(param+10);
}, timeout);
}
);
console.time('totalTime2');
Promise.all([(()=>{
return timeConsumingFunc(1).then(res1 => {
console.log(`f1 get response: ${res1}`);
return Promise.all([timeConsumingFunc(2),timeConsumingFunc(3)]);
}).then(([res2,res3])=>{
console.log(`f2 get response: ${res2}`);
console.log(`f3 get response: ${res3}`);
return timeConsumingFunc(4);
});
})(),timeConsumingFunc(5)]).then(([res4,res5])=>{
console.log(`f4 get response: ${res4}`);
console.log(`f5 get response: ${res5}`);
return timeConsumingFunc(6);
}).then(res6=>{
console.log(`f6 get response: ${res6}`);
console.timeEnd('totalTime2');
});
最终时间大致=MAX{(f1 + MAX(f2,f3) + f4), f5} + f6
添加回答
举报