Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda