📎 Webclip
takeWhile, takeUntil, takeWhat?
The post compares two RxJS ways to unsubscribe from Observables. It shows that takeWhile checks each incoming value against a condition, while takeUntil unsubscribes when another Observable emits. The difference matters because takeWhile needs one more emission to stop and can still trigger extra work before unsubscribing.
Reading notes#
takeWhileis used when the incoming value itself decides whether to stay subscribed.takeUntilis used when an outer Observable or event should end the subscription.- With
takeWhile, a long-lived Observable may need one extra notification before unsubscribing. - That extra notification can start side effects such as animation or an HTTP call before the unsubscribe happens.
- If the source never emits again,
takeWhilemay never run and the subscription can stay active. - Moving
takeWhileearlier in the pipe avoids some extra work, but it does not unsubscribe inner Observables returned by operators such asswitchMap. - The same placement issue applies to
takeUntil, so the unsubscribe operator should be last in the pipe. - The post presents long polling as a case where
takeWhilefits, because the current value can tell whether the process is completed. - For external triggers, the post recommends
takeUntilwith aSubject.
