{"id":1280,"date":"2023-08-04T05:00:00","date_gmt":"2023-08-04T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1280"},"modified":"2023-08-03T14:40:29","modified_gmt":"2023-08-03T21:40:29","slug":"rxjs-trick-emitting-from-an-observable-into-a-subject","status":"publish","type":"post","link":"http:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-trick-emitting-from-an-observable-into-a-subject\/","title":{"rendered":"RxJs trick: Emitting from an Observable into a Subject"},"content":{"rendered":"\n<p>Using a <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-subjects-when-and-why\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Subject<\/a> to emit data is something we do a lot in Angular applications, and more often than not, what we try to emit is the result of an HTTP request, such as:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"165\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-1-1024x165.png\" alt=\"\" class=\"wp-image-1281\" srcset=\"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-1-1024x165.png 1024w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-1-300x48.png 300w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-1-768x124.png 768w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-1-1536x247.png 1536w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-1.png 1714w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>While the above works perfectly well, it&#8217;s important to know that the subscribe method of an Observable takes either a function as a parameter (which is what I&#8217;ve done in the previous example) or an object that implements the <a href=\"https:\/\/rxjs.dev\/api\/index\/interface\/Observer\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>Observer<\/code> interface<\/a>. We touched on that interface earlier to illustrate exciting things we could do with the <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-tapobserver\/\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>tap<\/code> operator.<\/a><\/p>\n\n\n\n<p>The thing is that Subjects implement that interface, so we can simplify our earlier code into:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"100\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-2-1024x100.png\" alt=\"\" class=\"wp-image-1282\" srcset=\"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-2-1024x100.png 1024w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-2-300x29.png 300w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-2-768x75.png 768w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-2-1536x151.png 1536w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-2.png 1714w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>This isn&#8217;t just a syntax improvement, as writing less functions results in better overall Javascript performance. Less code to ship = less code to download = less code to interpret and store in memory for the browser = faster user experience. You can see a <a href=\"https:\/\/stackblitz.com\/edit\/stackblitz-starters-2difn3?\" target=\"_blank\" rel=\"noopener\" title=\"\">code example here on Stackblitz<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using a Subject to emit data is something we do a lot in Angular applications, and more often than not, what we try to emit is the result of an HTTP request, such as: While the above works perfectly well, it&#8217;s important to know that the subscribe method of an Observable takes either a function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,24],"tags":[],"class_list":["post-1280","post","type-post","status-publish","format-standard","hentry","category-rxjs","category-subjects"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1280"}],"version-history":[{"count":2,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1280\/revisions"}],"predecessor-version":[{"id":1284,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1280\/revisions\/1284"}],"wp:attachment":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1280"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}