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

98
Views
How to turn multiples promises dependent on each other's response into await and make sure all requests fire

I have an array of objects that has 3 items inside it. I'm iterating over it and during each loop i need to make 3 API calls that all depend on each other's response. So in total there should be 9 API requests completed.

const arrayOfObjects = [
  {
    first: 'thing',
    second: 'thing',
    third: 'thing'
  },
  {
    fourth: 'thing',
    fifth: 'thing',
    sixth: 'thing'
  },
  {
    seventh: 'thing',
    eight: 'thing',
    ninth: 'thing'
  },
]
const makeModuleAndBatteryPromises = () => {
  arrayOfObjects.map((obj) => {   

    createModules(obj.first).then((response) => {
      createBattery(response, obj.second).then(res => {
      assignAssets(res, obj.third).then(res => 'assignment done!');
      });
    });
  });
}
makeModuleAndBatteryPromises();

So looking at above code, it seems like i truly only have control over the first 3 API Calls in the 1st loop of arrayOfObjects. As i have assignAssets(res).then(res => 'assignment done!'); which will allow me to do some operation like refresh page or redirect once the 3rd promise is resolved in the first loop.

However i need to do some operation on the 9th/final promise. Here is what i tried trying to make it async await.


     const makeModuleAndBatteryPromises = async () => {
    
      const promises = arrayOfObjects.map(async obj => {
        const firstResponse = await createModules(obj.first);
        const secondResponse = await createModules(firstResponse, obj.second); 
        await assignAssets(secondResponse);
      });
    
        await Promise.all(promises)
         .then(res => //do some action after all 9 promises resolved)
      }

        makeModuleAndBatteryPromises()

Not quite achieving what i was expecting it to, can some1 please tell me what i'm missing?

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

0

if I understand correctly, you want the resolved value of the final assignAssets?

You've possibly confused yourself with a mix of async/await and .then

const makeModuleAndBatteryPromises = async() => {

    const promises = arrayOfObjects.map(async obj => {
        const firstResponse = await createModules(obj.first);
        const secondResponse = await createModules(firstResponse, obj.second);
        return assignAssets(secondResponse);
    });

    const res = await Promise.all(promises);
    const finalValue = res.at(-1); // or res[res.length-1];
    // do things with finalValue
}
makeModuleAndBatteryPromises()
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!