Utility

Angular: The Full Gamut Edition

Charlie Greenman
April 27, 2021
1 min read
razroo image

Utility functions in RxJS as are utility methods in programming, are often re-used operators, which are helpful for accomplushing routine RxJS tasks. However, the only common used utility on a day to day is tap.

tap

tap can be used to perform an action, or a side effect. A great example, and one used in an enterprise setting, is to navigate user to a particular place based on particular action. For instance, let's say the user log's out.

@Effect({ dispatch: false })
logOut = this.actions.pipe(ofType(LoginTypes.LogOut),tap([payload, username] => {this.router.navigate(['/']);}))

In the above, we are tapping into the action that specifies user has logged out. In addition to our reducer logic wiping clean all the data that was in the user's store, we are also navigating the user to the default rout. This a perfect scenario for using tap, as we want to create a side effect of navigating to a route. However, we do want to return an observable/action (i.e. do not want to use map).

Subscribe to the Razroo Angular Newsletter!

Razroo takes pride in it's Angular newsletter, and we really pour heart and soul into it. Pass along your e-mail to recieve it in the mail. Our commitment, is to keep you up to date with the latest in Angular, so you don't have to.

More articles similar to this

footer

Razroo is committed towards contributing to open source. Take the pledge towards open source by tweeting, #itaketherazroopledge to @_Razroo on twitter. One of our associates will get back to you and set you up with an open source project to work on.