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

83
Views
Calling async function multiple times with callback response of previous run

I need to run a function for each item of the array... The problem is, for each run (except the first), the response from the previous run is supposed to be one of the parameters... How do I make sure the next iteration of the function isn't executed till the response from the last one is in place?

I am currently doing it as something like this, but no success:

array --> array with n items
callResponse --> array with (n-1) items
callResponse[0] is a required parameter for the function executing for array[1] and so on....

array = [ ........ ]
callResponse = [....empty x (n-1)....]

for (var i in array) {
var body = {
            parameter1: array[i],
            parameter2: i = 0 ? '' : callResponse[i-1],
           }
axios.get('', body).then((response) => { callResponse[i] = response.data.x }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It can easily be done with async/await syntax. Just wait until the request finishes then continue the next request:

(async () => { // await keyword only accept in an async function
  const array = [];
  const result = [];

  for (const item of array) { // for..of instead of for..in
    const body = {
      parameter1: item,
      parameter2: result[result.length - 1] || '', // get the last response
    };

    const res = await axios.post('url', body); // wait...
    result.push(res.data.x); // save the response and continue
  }

  console.log('Result: ', result); // all responses
})();
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!