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

162
Views
Get the final value in async loop

If anyone found this question is a dup, please comment a link to that, or any suggestions would be appreciated.

  • The request is to fetch data bases on an array of id.

  • Function getWorkhoursById() has already been configured with axios, including the Bearer and clientId, so I want to reuse it for this purpose.

  • So, now the result will be filled up with array of ids for each fetch, but it takes some time to reach the final value. My question is how can I retrieve the final value of arrayOfIds (with all the ids in the last iteration) to perform the next task.

  • I tried the for loop method - check the last value of it, but I would like to do this in a more proper, asynchronous way.

const arrayOfIds = [];
let result = [];

const foo = (item, index, id) => {
    return new Promise((resolve, reject) => {
      setTimeout(async () => {
        const data = await getWorkhoursById(id);
        if (data.length === 0) result.push(item);
        resolve();
      }, 1000 * (index + 1)); // force async function to execute in order
    });
  };
  const bar = () => {
      return arrayOfIds.forEach(async (item, index) => {
        names(item, index, id).then(() => {
          console.log(result, "result"); // <- need the last value of result
      });
    });
  };

about 4 years ago · Juan Pablo Isaza
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!