I am currently learning about RXJS and its schedulers. In a first example, I tried an example of a course like following (I am aware that the of()-operator in this example is deprecated):
console.log('start');
let queue = of('QueueScheduler (synchronous)', queueScheduler);
let asap = of('AsapScheduler (async micro task)', asapScheduler);
let async = of('AsyncScheduler (async task)', asyncScheduler);
merge(async, asap, queue).subscribe(val => console.log(val));
console.log('end');
which results in following output:
start
QueueScheduler (synchronous)
end
AsapScheduler (async micro task)
AsyncScheduler (async task)
This is as expected, since in my understanding, the order of processing is Sync Task Queue -> Async MicroTask Queue -> Async Task Queue.
However, in a second example using the observeOn()-operator, I don't understand the logs. Here is the example:
console.log('start');
from([1, 2], queueScheduler)
.pipe(
tap(val => console.log(`value ${val}`)),
observeOn(asyncScheduler),
tap(val => console.log(`double ${val * 2}`)),
observeOn(asapScheduler),
tap(val => console.log(`triple ${val * 3}`))
)
.subscribe();
console.log('end');
In this case, following output gets generated:
start
value 1
value 2
end
double 2
triple 3
double 4
triple 6
Why does the second and third tap get executet simultanously? Is the second observeOn()-operator overriding the first one? In my understanding of the queues, the output should be
start
value 1
value 2
end
triple 3
triple 6
double 2
double 4
because the second tap()-operator is pushed into the asyncScheduler, while the third one is pushed in the asapScheduler, which should execute before the asyncScheduler. What am I missing here?