{"id":1702,"date":"2023-10-31T05:00:00","date_gmt":"2023-10-31T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1702"},"modified":"2023-10-30T15:08:33","modified_gmt":"2023-10-30T22:08:33","slug":"async-pipe-signals-and-change-detection","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/async-pipe-signals-and-change-detection\/","title":{"rendered":"Async pipe, Signals, and Change Detection"},"content":{"rendered":"\n<p>Yesterday, we covered what <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/changedetectorref-markforcheck\/\" target=\"_blank\" rel=\"noopener\" title=\"\">markForCheck()<\/a> can do in terms of change detection and why it could be needed at times.<\/p>\n\n\n\n<p>Regarding best practices, I often mention using the <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/async-pipe-syntax-tricks\/\" target=\"_blank\" rel=\"noopener\" title=\"\">async pipe<\/a> or <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/perf-and-template-syntax-example-3\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Signals<\/a> to improve change detection and performance in our apps. Why is that? That&#8217;s because both the <code>async<\/code> pipe and Signals have some extra code to help Angular detect changes.<\/p>\n\n\n\n<p>Here&#8217;s what we can find in the <a href=\"https:\/\/github.com\/angular\/angular\/blob\/16.2.11\/packages\/common\/src\/pipes\/async_pipe.ts#L166-L174\" target=\"_blank\" rel=\"noopener\" title=\"\">source code of the <code>async<\/code> pipe<\/a>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"535\" height=\"203\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-28.png\" alt=\"\" class=\"wp-image-1704\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-28.png 535w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-28-300x114.png 300w\" sizes=\"auto, (max-width: 535px) 100vw, 535px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The last line of code indicates that whenever an async pipe receives a new value, it marks the corresponding view for check, triggering the need for change detection. That&#8217;s the reason why the <code>async<\/code> pipe works well even when used in an <code>OnPush<\/code> component: Its internals override the behavior of whatever change detection strategy we use.<\/p>\n\n\n\n<p>Signals do <a href=\"https:\/\/github.com\/angular\/angular\/blob\/16.2.11\/packages\/core\/src\/signals\/src\/graph.ts#L255\" target=\"_blank\" rel=\"noopener\" title=\"\">something similar<\/a> by marking their consumers (or &#8220;subscribers,&#8221; in a sense) as dirty when the Signal value changes:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"528\" height=\"131\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-29.png\" alt=\"\" class=\"wp-image-1705\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-29.png 528w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/10\/image-29-300x74.png 300w\" sizes=\"auto, (max-width: 528px) 100vw, 528px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>As a result, and back to the point I was making yesterday, Instead of trying to use markForCheck() directly in our code, we should rely on the tools designed to do it automatically and in an optimized fashion for us. <\/p>\n\n\n\n<p><strong>Conclusion<\/strong>: Always use the <code>async<\/code> pipe with RxJs (you can <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/async-pipe-syntax-tricks\/\" target=\"_blank\" rel=\"noopener\" title=\"\">always do so<\/a>) or use Signals for an even better experience.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday, we covered what markForCheck() can do in terms of change detection and why it could be needed at times. Regarding best practices, I often mention using the async pipe or Signals to improve change detection and performance in our apps. Why is that? That&#8217;s because both the async pipe and Signals have some extra [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,21,14,29],"tags":[],"class_list":["post-1702","post","type-post","status-publish","format-standard","hentry","category-angular","category-architecture","category-pipes","category-signals"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1702","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=1702"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1702\/revisions"}],"predecessor-version":[{"id":1706,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1702\/revisions\/1706"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}