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

171
Visualizações
SetTimeOut inside a Javascript Promise

I have this code snippet , basically I want to add some interval in between calling myfunc for each element in myCol .

         Promise.all(
                _.map(myCol, (o) ->
                  setTimeout =>
                    myfunc(o)
                    10000)
              )

With above implementation , myfunc is getting called without any interval

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is that the map function is setting the timeouts all at once. What you need to do is have the map function return after (n+1) lots of 10 seconds where n is the index of the element in myCol.

As I don't have _.map, I'll show you with array.prototype.forEach:

const myCol = [1,2,3,4];
const myFunc = console.log;

myCol.forEach((o,i) => setTimeout(myFunc, (i+1)*1000, o));

So, (i+1)*1000 set the timeouts to run i seconds after the call to forEach ends. For your code, change 1000 to 10000.

Of course if you don't want a delay for the first one, just do i*1000.

Also note that I'm putting o as the last parameter in setTimeout. The way you have it now, the function being called by setTimeout is the result of calling myfunc not myfunc itself.

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