How to chain a Promise.all with other Promises?

后端 未结 2 458
说谎
说谎 2021-02-06 22:58

I want to execute my code in the following order:

  1. Promise 1
  2. Wait for 1 to be done, then do Promise 2+3 at the same time
  3. Final function waits for
相关标签:
2条回答
  • 2021-02-06 23:04

    I know it's an old thread, but isn't

    () => {return Promise.all([getPromise2(), getPromise3()]);}
    

    a little superfluous? The idea of fat arrow is that you can write it as:

    () => Promise.all([getPromise2(), getPromise3()])
    

    which makes the resulting code somewhat clearer:

    getPromise1().then(() => Promise.all([getPromise2(), getPromise3()]))
    .then((args) => console.log(args)); // result from 2 and 3
    

    Anyway, thanks for the answer, I was stuck with this :)

    0 讨论(0)
  • 2021-02-06 23:11

    Just return Promise.all(...

    getPromise1().then(() => {
      return Promise.all([getPromise2(), getPromise3()]);
    }).then((args) => console.log(args)); // result from 2 and 3
    
    0 讨论(0)
提交回复
热议问题