{"id":839,"date":"2023-04-21T05:00:00","date_gmt":"2023-04-21T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=839"},"modified":"2023-04-19T17:54:04","modified_gmt":"2023-04-20T00:54:04","slug":"rxjs-takeuntil-and-takewhile-operators","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-takeuntil-and-takewhile-operators\/","title":{"rendered":"RxJs takeUntil and takeWhile operators"},"content":{"rendered":"\n<p>Our Rxjs operators of the week are <a href=\"https:\/\/rxjs.dev\/api\/operators\/takeUntil\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>takeUntil<\/code><\/a> and <code><a href=\"https:\/\/rxmarbles.com\/#takeWhile\" target=\"_blank\" rel=\"noopener\" title=\"\">takeWhile<\/a><\/code>. These operators are the opposite of <code><a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-skipwhile-operator\/\" target=\"_blank\" rel=\"noopener\" title=\"\">skipWhile<\/a><\/code> covered last week: They let the values from the source Observable pass through until a condition is met.<\/p>\n\n\n\n<p><code>takeUntil<\/code> is based on a second Observable and stops emitting values when that second Observable emits for the first time, as illustrated in this <a href=\"https:\/\/rxmarbles.com\/#takeUntil\" target=\"_blank\" rel=\"noopener\" title=\"\">marble diagram<\/a>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1005\" height=\"427\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-35.png\" alt=\"\" class=\"wp-image-840\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-35.png 1005w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-35-300x127.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-35-768x326.png 768w\" sizes=\"auto, (max-width: 1005px) 100vw, 1005px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p><code>takeWhile<\/code> is very similar but uses a predicate instead of an Observable to decide when to stop emitting from the source Observable:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1005\" height=\"313\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-36.png\" alt=\"\" class=\"wp-image-842\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-36.png 1005w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-36-300x93.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-36-768x239.png 768w\" sizes=\"auto, (max-width: 1005px) 100vw, 1005px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The primary use case for <code>takeUntil<\/code> with Angular is to automate unsubscriptions from Observables, even though I recommend <a href=\"https:\/\/blog.angulartraining.com\/how-to-automatically-unsubscribe-your-rxjs-observables-tutorial-2f98b0560298\" target=\"_blank\" rel=\"noopener\" title=\"\">less verbose approaches to automating unsubscriptions<\/a>, such as using the <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/how-to-avoid-memory-leaks-with-rxjs-observables\/\" target=\"_blank\" rel=\"noopener\" title=\"\">async pipe<\/a> :<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"616\" height=\"286\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-37.png\" alt=\"\" class=\"wp-image-843\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-37.png 616w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-37-300x139.png 300w\" sizes=\"auto, (max-width: 616px) 100vw, 616px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above code uses a <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-subjects-when-and-why\/\" target=\"_blank\" rel=\"noopener\" title=\"\">subject<\/a> to emit a value when the component gets destroyed, thanks to the ngOnDestroy lifecycle hook. Then, that same subject is used by <code>takeUntil<\/code> to complete the source Observable.<\/p>\n\n\n\n<p>Here is a different <a href=\"https:\/\/stackblitz.com\/edit\/angular-takeuntil-test-8tkcng?file=src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.html\" target=\"_blank\" rel=\"noopener\" title=\"\">example of <code>takeUntil<\/code> in action on Stackblitz<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Our Rxjs operators of the week are takeUntil and takeWhile. These operators are the opposite of skipWhile covered last week: They let the values from the source Observable pass through until a condition is met. takeUntil is based on a second Observable and stops emitting values when that second Observable emits for the first time, [&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-839","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\/839","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=839"}],"version-history":[{"count":3,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/839\/revisions"}],"predecessor-version":[{"id":845,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/839\/revisions\/845"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}