The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Utility operator: do

Utility operator: do

4:25
We just saw map which is a transformation operator. There are a couple of categories of operators, such as filtering, combination, flattening, etc. One of these categories is the utility operators. The most important utility operator is `do`, useful for debugging.
Watch this lesson now
Avatar
egghead.io

We just saw map which is a transformation operator. There are a couple of categories of operators, such as filtering, combination, flattening, etc. One of these categories is the utility operators. The most important utility operator is do, useful for debugging.

Avatar
Sam

May I ask why the both do functions outputted before the actual map function e.g you got 'before, after, next' on your console logs rather than 'before, next, after' even though the functions were chained in the order .do > .map > .do ?

In reply to egghead.io
Avatar
Andre

Hi Sam, that sequence of logs is due to .do > ... > .do > .subscribe. The map itself doesn't cause any console log, it is subscribe that cause a console.log.

In reply to Sam
Avatar
kefkef5

Hello Andre,

I love the series you put up here, every lesson I learn about RxJS makes me fall in love more and more with Reactive Programming. I have a question concerning the do() operator.
I read that all unpure behavior like logging, changing button states, writing to local storage should be wrapped in the do() operator. Is this the general practice or are there better alternatives for this?

Kind regards,
Kevin

Avatar
Andre

Hi Kevin. Glad to hear you are learning!
I would recommend putting impure code as such in subscribe(), not do(). There are a few cases where you could use do() for impure code, but mostly they should go to subscribe(). There is an upcoming course I made (already fully recorded) which will soon be put on Egghead that teaches exactly about do() vs subscribe(). Stay tuned.

In reply to kefkef5
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?