I'm new to RxJS, and is learning the Schedulers topic, what confuse me is the difference between queueScheduler and null scheduler.
I have read the RxJS docs and some blogs about this topic.
I thought I've got the idea about the difference, but the following code just break all my 'understandings'.
combineLatest([
// scheduled([1, 2], asyncScheduler), // [1, 10], [2, 10]
// scheduled([1, 2], queueScheduler), // just [2, 10]
from([1, 2]), // just [2, 10]
from([10]),
]).subscribe(console.log);
According to this answer, using the queue scheduler turns the streams somehow 'breadth first', so I was expecting [1, 10], [2, 10] when using the queueScheduler just the same as asyncScheduler in this case.
Any one can explain why this happen, or am I doing something wrong?
Thanks!
To be honest I'm not very confident how the queueScheduler is implemented and what is its typical real world use-case.
One example where queueScheduler and asyncScheduler differ is when expecting synchronous behavior while making recursive emissions that trigger another emission. Without any scheduler this would create an infinite loop but with queueScheduler I can still have synchronous code.
import { observeOn, asyncScheduler, queueScheduler, Subject } from 'rxjs';
const subject = new Subject<number>();
console.log('before');
const sub = subject
.pipe(
observeOn(asyncScheduler),
// observeOn(queueScheduler),
)
.subscribe(v => {
console.log(v);
subject.next(v + 1);
if (v > 10) {
sub.unsubscribe();
}
});
subject.next(1);
console.log('after');
Live demo: https://stackblitz.com/edit/rxjs-945r4b?devtoolsheight=60&file=index.ts
If you uncomment observeOn(queueScheduler) you'll get the same result but the chain is invoked asynchronously.
Regarding using null as a scheudler I'm not sure what bahavior is expected. AFAIK a lot has changed since RxJS 5 and how it works with schedulers by default. The answer you mention was posted in 2018 so I would expect that what's written there might be obsolete.