It's because you are putting a Delay
on the result stream. (The value for n passed to ExponentialBackoff
on the second iteration is 1, giving a delay of 1 second.)
Delay
operates on source, but the source proceeds as normal. Delay
schedules the results it receives to be emitted after the specified duration. So the subscriber is getting the results after the logic of Generate has run to completion.
If you think about it this is how Delay
must be - otherwise Delay
would be able to somehow interfere with upstream operators!
It is possible to interfere with upstream operators (without throwing exceptions), by being a slow consumer. But that would certainly be a very bad way for a simple Delay
to behave.
I don't think the Delay
is what you intend here - because Delay
doesn't delay it's subscription. If you use DelaySubscription
instead, you'll get what you're after I think. This is what's used in the linked question too.
Your question provides a great illustration of the difference between Delay
and DelaySubscription
! It's worth thinking about Defer
in here too.
The distinction between these three is subtle but significant, so let's summarize all three:
Delay
- Calls target operator immediately to get anIObservable
, on itsSubscribe
callsSubscribe
on target immediately, schedules events for delivery after specified delay on the specifiedScheduler
.DelaySubscription
- Calls target operator immediately to get anIObservable
. On itsSubscribe
schedulesSubscribe
on target for execution after specified delay on the specifiedScheduler
.Defer
- Has no target operator. OnSubscribe
runs provided factory function to get targetIObservable
and immediately callsSubscribe
. There's no delay added, hence noScheduler
to specify.