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

116
Views
Combination of rxjs and setTimeout creates unexpected runtime behaviour

I have some problems to understand the difference between the following two snippets.

const a = Observable.create(
  o => {
    setTimeout(
      ()=>{ 
        console.log("now 3");
        o.next(3);
        setTimeout(()=>{ console.log("and now 5"); o.next(5)}, 1500);}
      , 1500);
   
  }
);
const a2 = Observable.create(
  o => {
    setTimeout(
      ()=>{ 
        console.log("here is 4");
        o.next(4);
        setTimeout(o.next(6), 1500);}
      , 1500);
  }
);

https://stackblitz.com/edit/rxjs-byqgnh?devtoolsheight=60

I removed in the second block only the console log.

The first block works like expected: first number is emitted after timeout, second number too.

The second block emitts both numbers simultanious. Can anybody explain the difference?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

When you do

setTimeout(()=>{ console.log("and now 5"); o.next(5)}, 1500);

you are passing ()=>{ console.log("and now 5"); o.next(5)} as a callback function to setTimeout API, which will be probably trigger after 1.5sec. And Its fine since this is what you wanted to happen.

But when you do

setTimeout(o.next(6), 1500)

You are executing o.next(6) and emitting value right before you are passing it as a callback function to setTimeout, but when you see It's not a function signature, what o.next(6) generates is a undefined value which will be pass as a callback and chances you will get Callback must be a function error.

So, if you want o.next(6) to be passed as a callback function to setTimeout and emit value after 1.5 sec then you can pass it as

setTimeout(() => o.next(6), 1500)

or as

setTimeout(function(){
   o.next(6);
}, 1500)
about 4 years ago · Juan Pablo Isaza Report

0

setTimeout takes a callback

The following with generate a runtime error

setTimeout(o.next(6), 1500);

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function.

as the return type of n.next is undefined. Observables, however, catch errors and emit them as error events for you. So you don't see that in your example.

To fix this, you could give setTimeout a callback function instead.

setTimeout(() => o.next(6), 1500);
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!