↓ Ir para o conteúdo principal

← todas as notas

📎 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
#

  • takeWhile is used when the incoming value itself decides whether to stay subscribed.
  • takeUntil is 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, takeWhile may never run and the subscription can stay active.
  • Moving takeWhile earlier in the pipe avoids some extra work, but it does not unsubscribe inner Observables returned by operators such as switchMap.
  • 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 takeWhile fits, because the current value can tell whether the process is completed.
  • For external triggers, the post recommends takeUntil with a Subject.