How to use Promise to meet the following requirements

f2 and f3 depend on the results of F1, f4 on the results of f2 and f3, and f6 on the results of f3 and f4 and f5? What is the most efficient way to write
?

    f1 
  /    \
f2      f3      f5
  \    / \     /
    f4    \   /
       \   \ / 
          f6
Mar.20,2021

. Ladies and gentlemen upstairs, show off your skills. Why mix all those dependencies together? Is it really that expensive to use a few variables? Can the code be maintained after it is written?

ask the subject

What is the most efficient way to write
?

I believe that programmers' efficiency also counts .


f5f5f5>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');
});

final time roughly = MAX {(F1 + MAX (f2Magnef3) + f4), f5} + f6

Menu