{"id":980,"date":"2023-05-22T05:00:00","date_gmt":"2023-05-22T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=980"},"modified":"2023-05-19T20:42:15","modified_gmt":"2023-05-20T03:42:15","slug":"rxjs-tapobserver","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-tapobserver\/","title":{"rendered":"RxJs TapObserver"},"content":{"rendered":"\n<p>In my last post, I covered <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-subscribe-callback-functions\/\" target=\"_blank\" rel=\"noopener\" title=\"\">how we can define multiple callback functions when subscribing<\/a> to an Observable (<code>next<\/code>, <code>error<\/code>, <code>complete<\/code>). I mentioned that those callbacks are available when we call the <code>subscribe()<\/code> method, which goes against our philosophy of <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/how-to-avoid-memory-leaks-with-rxjs-observables\/\" target=\"_blank\" rel=\"noopener\" title=\"\">avoiding subscriptions as much as possible because they can lead to memory leaks<\/a>.<\/p>\n\n\n\n<p>In previous newsletters, I covered how the <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-tap-operator\/\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>tap<\/code> operator<\/a> can help avoid subscriptions by &#8220;spying&#8221; on an Observable.<\/p>\n\n\n\n<p>Well, <code>tap<\/code> is a simple gift that keeps on giving. The operator supports an <code><a href=\"https:\/\/rxjs.dev\/api\/index\/interface\/TapObserver\" target=\"_blank\" rel=\"noopener\" title=\"\">TapObserver<\/a><\/code> interface that can be used instead of the callback function we used in our <a href=\"https:\/\/stackblitz.com\/edit\/angular-ivy-5aafly?file=src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.html\" target=\"_blank\" rel=\"noopener\" title=\"\">earlier examples<\/a>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"509\" height=\"317\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-26.png\" alt=\"\" class=\"wp-image-982\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-26.png 509w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-26-300x187.png 300w\" sizes=\"auto, (max-width: 509px) 100vw, 509px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>As a result, we can register several side effects in our code based on any of these notifications as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"633\" height=\"213\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-27.png\" alt=\"\" class=\"wp-image-983\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-27.png 633w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-27-300x101.png 300w\" sizes=\"auto, (max-width: 633px) 100vw, 633px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Most of these notifications are explicit in what they do, but let&#8217;s document all six of them for the sake of clarity:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>next<\/code>: Invoked whenever the Observable receives new data<\/li>\n\n\n\n<li><code>error<\/code>: Invoked whenever an error occurs within the Observable<\/li>\n\n\n\n<li><code>complete<\/code>: Invoked when the Observable completes and stops emitting any new values.<\/li>\n\n\n\n<li><code>subscribe<\/code>: Invoked whenever a new subscriber subscribes to the Observable<\/li>\n\n\n\n<li><code>unsubscribe<\/code>: Invoked whenever a subscriber unsubscribes from the Observable<\/li>\n\n\n\n<li><code>finalize<\/code>: Invoked whenever the Observable is done, either because of an error, of completion, or unsubscription. It&#8217;s like a catch-all notification to mark the end of an Observable.<\/li>\n<\/ul>\n\n\n\n<p>You can see an example of such <code>TapObserver<\/code> in action <a href=\"https:\/\/stackblitz.com\/edit\/at-tap-observer?file=src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.html\" target=\"_blank\" rel=\"noopener\" title=\"\">here on Stackblitz<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last post, I covered how we can define multiple callback functions when subscribing to an Observable (next, error, complete). I mentioned that those callbacks are available when we call the subscribe() method, which goes against our philosophy of avoiding subscriptions as much as possible because they can lead to memory leaks. In previous [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,10],"tags":[],"class_list":["post-980","post","type-post","status-publish","format-standard","hentry","category-operators","category-rxjs"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/980","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=980"}],"version-history":[{"count":4,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/980\/revisions"}],"predecessor-version":[{"id":1000,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/980\/revisions\/1000"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}