Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
Correct way to handle max of independent and dependent async/await jobs (Promises)

I have four asynchronous processes that fetch from different sources, namely process A, B, C and D.

Process A, B and C are independent of each other, but D depends on data from C.

As far as possible, I want A and B to be fetched independently on C and D, but I want to keep the result of A, B, C and D for the procedure that follows.

So I'm trying to come up with the correct way to express this functionality.

Something like

const results = await Promise.allSettled([A, B, C]);
const resultD = await D(results[2].value);

means that I'm waiting to perform D until A, B, C are all finished, but in this case A or B might take significantly more time than C.

Likewise,

const results = await Promise.allSettled([A, B, C, D(await C)]);

Doesn't seem quite right? I don't want C performed twice, and I also want to keep the result.

What would be the correct way to go about solving this elegantly?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can group C and D together and resolve it with A and B.

Let's understand the snippet below:

Consider the functions A, B, C & D equivalent to fetching from four different APIs. Also, notice how D needs some data, that it would get from C.

And in the main function we're resolving A, B independently and we're resolving D only after C has finished.

const sleep = (delay, data) => new Promise(res => setTimeout(() => res(data), delay));

const A = () => sleep(100).then(() => "A")
const B = () => sleep(200).then(() => "B")
const C = () => sleep(100).then(() => "C")
const D = (inp) => sleep(100).then(() => inp + "D")


function main() {
  const promises = [A(), B(), C().then(res => D(res))];
  Promise.all(promises).then(console.log);
}

main();

You can also use Promise.allSettled but make sure you understand the difference between allSettled and all.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!