How to display the result of the operator do after the main action?

This code:
Observable.timer(0, 3000).do((v) => console.log('tick', v)).subscribe((r) => {
console.log(r);
 });


Outputs this:
tick 0
0
tick 1
1
tick 2
2
tick 3
3


But I do not understand why in the record of the operator do is AFTER the timer, and is output TO the timer. Is it possible to make so that the result of the statement do output after the number, which generates a timer?

And anyway, nobody finds strange the effect of this operator?..
June 7th 19 at 14:24
1 answer
June 7th 19 at 14:26
Solution
The operator do not affect the sequence, you simply "hook" into it, get its current data and can start any side effects on the basis of these data (the execution result of the function do not affect the data that will be in the next operator or the final subscription). The timer you signed after the statement, do so first execute the actions in the do, then subscribe.

Similarly, every action performed by any other operator before you get to subscribe, but other operators or otherwise change the original sequence.

To achieve the desired behavior, you can record a sequence into a variable and subscribe to her 2 times in the right order:
const obs = Observable.timer(0, 3000);
obs.subscribe(i => console.log(i));
obs.subscribe(i => console.log('tick', i));

Find more questions by tags JavaScriptAngular