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

225
Views
Use Axios .then() with for loop index variable

I want to iterate some page URLs, stored in an array, and retrieve them to do an operation over their html file. Since I need to store a result from said operation for each page to use them the next time I call the function, I want to pass the index (i) in the onResponse call so that it can assign the result to its proper place in the array. But instead of using the i value from the respective i-th iteration, this function call uses pages.length (i's last value). I know why this happens, but I don't know what would be a good solution. Any help here?

async function checkPages(){
const dateObj = new Date();
console.log(pages.length)
console.log(`Time: ${dateObj.toTimeString()}\n`);
for (var i = 0; i < pages.length; i++){
    axios.get(pages[i], { httpsAgent })
      .then(response => onResponse(response, i)
    )
      .catch(function (error) {
        onError(error)
      });
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

try using let instead of var

for (let i = 0; i < pages.length; i++) {

let are block scoped and var are function scoped.

Since you are calling an async method, using var it will finish the for loop and pass the last value to the callback. But using let it will keep the variable assigned in the block scope in the callback.

Here is the definition for let, and here is for var.

about 4 years ago · Juan Pablo Isaza Report

0

axios.get is a promise so your foreach is kicking off all http requests and i gets to the array length before you get any response from your API.

Try:

async function checkPages() {
  const results = await Promise.all(
    pages.map((page) =>
      axios.get(page, { httpsAgent }).catch((error) => {
        onError(error);
      })
    )
  );
  results.forEach((response, i) => {
    onResponse(response, i);
  });
}
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!