Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

456
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!