'Complex serial and parallel promises resolution

So I have this function that does 4 things, which have to be sequenced the following way:

- promise1
- promiseAfter1
// In parallel
- promise2
- promiseAfter2

To get the serial promises I shall write:

async sequence1() {
  const result = await promise1();
  const finalResult = await promiseAfter1(result);
}
async sequence2() {
  const result = await promise2();
  const finalResult = await promiseAfter2(result);
}

And then:

async finalSequence() {
  await Promise.all([sequence1, sequence2]);
}

But in order to achieve this, I had to create two sub-functions (sequence1 and sequence2) which I'd like to avoid for code readability among other reasons.

Is there a way to achieve the same result without having to create two sub-functions?

Shouldn't there be something like Promise.sequence that would sequentially resolve promises?

This would write:

Promise.all([
  Promise.sequence([promise1, promiseAfter1]),
  Promise.sequence([promise2, promiseAfter2]),
])

Cheers



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source