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

463
Vistas
observable - how do I return values using forEach and setTimeout? (nothing to do with Rxjs)

I'm new to observablehq notebook. I found this is very helpful tool for me to prototype my visuals.

My question is How do I iterate an array and return values with setTimeout of javascript?

My approach is as below.

  1. array is declared

    arr = [1,2,3,4,5]

  2. iterate the array and return values

       arr.forEach((d,i)=>{
     setTimeout(()=>{
       return d
     },i*1000)})
    

How do I achieve this?

I saw similar posting using Rxjs but I don't use that. Is there a basic / elementary way of achieving this?

I think the reason that this isn't working is something to do with closure. But can't think of solving the issue.

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

0

You can try the solution below

    myArr = [1, 2, 3, 4, 5];

//Now, iterate over the array with setTimeout
myArr.forEach(function(d, i) {
    setTimeout(function() {
        console.log(d);
        return d;
    }, 1000 * i);
});
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