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

208
Visualizações
JavaScript, detect when a promise in an array of promises is resolved then do something with it

I have the following code :-

      for (let i = 0; i < fdlist.length; i++) {
        let request = fetch(`${domain}/segment?mode=${mode}&type=${type}`,  {    method: 'POST', body: fdlist[i]   }).then((res) => res.json());
        requestlist.push(request)
        console.log(requestlist)
        }
        const allData = Promise.all(requestlist);
        allData.then(async (res) => { 

It sends multiple fetch requests and pushes each one of them to a list, then I do a Promise.all to wait for all of them to resolve and process all the results at once.

My question is, how can I process the promises that resolve right away instead of waiting for all the promises to resolve to begin processing?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

just loop through the promises and add "then" clauses:

for (const p of requestlist) {
  p.then(result => ...)
}

// you can always still wait for them all if you want
Promise.all(requestlist).then(...)

you can add your then at the start when you fetch:


for (...) {
  let request = fetch(...)
  const processed = request.then(response => ...)
  requestlist.push(processed)
}

// wait for it all here if you still want
Promise.all(requestlist).then(...)
about 4 years ago · Santiago Gelvez 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