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

239
Views
How to resolve nested promises with Promise.all()

I am trying to write something like this but not able to resolve inner promises.

  const traceSamplesRequests = transactionTypes.map(async (transactionType) => {
    // This part should be synchronous since we need transactionNames
    const transactionNames = await api.getTransactionNames();
    return transactionNames.map((transactionName) => api.getTraceSamples());
  });

And I want to write with lambda functions with Promise.all not imperative loops.

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

0

So, .map() is not promise-aware. It calls the callback and just dutifully collects the return value from the callback and advances to the next iteration of the loop. Since your callback is async with an await in it, that return value is an unresolved promise. So, what you get back from .map() is an array of promises.

So, there is no way to use .map() or any of the array iteration methods without then using something to wait for the promises.

It sounds like you already know this, but with the code you have, you would insert two Promise.all() statements:

  const traceSamplesRequests = await Promise.all(transactionTypes.map(async (transactionType) => {
    // This part should be synchronous since we need transactionNames
    const transactionNames = await api.getTransactionNames();
    return Promise.all(transactionNames.map((transactionName) => api.getTraceSamples()));
  }));

That would get you an array of values (and the parent function would have to be async). This will run all the asynchronous operation in the loop in parallel (all in flight at the same time).

Your other choices are to use a for or while loop which will suspend loop iteration for each await and will run your asynchronous operations in sequence (second iteration of the loop not running until the first is done).

Or, you can write some sort of helper function that would be a promise-aware version of .map(). You'd probably just either use for or .map() with Promise.all() inside the helper function though.

The Async promise library has multiple such helper functions as choices to use. They are designed to work with either promises or plain callbacks.

about 4 years ago · Juan Pablo Isaza Report

0

I actually made it work like this:


  const traceSamplesRequests = await Promise.all(
    transactionTypes.map(async (transactionType) => {
      const transactionNames = await api.getTransactionNames({ transactionType });
      return await Promise.all(transactionNames.map((transactionName) => api.getTraceSamples({ transactionType })));
    })
  );
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!