subscribeOn
Asynchronously subscribes Observers to this Observable on the specified SchedulerLike
.
Parameters
scheduler |
The |
delay |
Optional. Default is Type: |
Returns
MonoTypeOperatorFunction<T>
: The source Observable modified so that its subscriptions happen on the specified SchedulerLike
.
.
Description
With subscribeOn
you can decide what type of scheduler a specific Observable will be using when it is subscribed to.
Schedulers control the speed and order of emissions to observers from an Observable stream.
Example
Given the following code:
Both Observable a
and b
will emit their values directly and synchronously once they are subscribed to.
This will result in the output of 1 2 3 4 5 6 7 8 9
.
But if we instead us the subscribeOn
operator declaring that we want to use the async
for values emited by Observable a
:
The output will instead be 5 6 7 8 9 1 2 3 4
.
The reason for this is that Observable b
emits its values directly and synchronously like before
but the emissions from a
are scheduled on the event loop because we are now using the async
for that specific Observable.