Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
How to make for loop wait for certain period for every interval till array is completed

I am building a small prototype like feeds. I have an array of many items. For each item in an array, I need to make an API call and load results.

It's a performance issue if I have too many records and too many APi calls to make.

I am trying to stop my loop for every 2-3 API calls for 100ms and then resume execution till array is completed.

I made a POC but it doesn't work well. Can you help me with whats wrong in below code

// work in progress

var arr = ["val1", "val2", "val3", "val4", "val5", "val6"];

let breakCounter;
async function seriesAPI(arr) {
  breakCounter = 2;
  for (let i = 0; i <= arr.length; i++) {
    if (breakCounter - arr.indexOf(arr[i]) === 0) {
      await interval(arr[i]);

      breakCounter = breakCounter + 2;
    } else {
      makeAPI(arr[i])
      //console.log(arr[i]);
    }
  }
}

function makeAPI(val) {
  console.log(val);
  // fetch(url, {method: 'POST', body: JSON.stringify(arr[i])}).then(res => console.log(res));
}

function interval(arrval) {
  console.log("Buffer time");
  setTimeout(() => {
    makeAPI(arrval)
  }, 1000);
}

seriesAPI(arr);

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I suggest you to wrap your interval method as Promise and call it on loop iteration when it's needed before request fetching.

var arr = ["val1", "val2", "val3", "val4", "val5", "val6"];

async function seriesAPI(arr) {
  for (let i = 0; i <= arr.length; i++) {
    if (!(i % 3)) { // on each 3rd iteration
      await interval(100); // make 100ms delay
    }
    makeAPI(arr[i])
    // console.log(arr[i]);
  }
}

function makeAPI(val) {
  console.log(val);
  // fetch(url, {method: 'POST', body: JSON.stringify(arr[i])}).then(res => console.log(res));
}

function interval(ms) {
  return new Promise(r => setTimeout(r, ms)); 
  // auto-resolve after [ms] milliseconds  
}

seriesAPI(arr);

about 4 years ago · Juan Pablo Isaza Denunciar

0

No need for async

var arr = ["val1", "val2", "val3", "val4", "val5", "val6"];
let cnt = 0;

function makeAPI() {
  if (cnt >= arr.length) return 
  console.log(arr[cnt]);
  // fetch(url, {method: 'POST', body: JSON.stringify(arr[cnt])})
  //  .then(res => setTimeout(makeAPI,cnt%3 === 0 ? 3000: 100));
  cnt++;
  setTimeout(makeAPI,cnt%3 === 0 ? 3000: 100);
}

makeAPI()

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda