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

168
Vistas
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 Respuestas
Responde la pregunta

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 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