DEV Community

Discussion on: RxJS Autorun Intro

Collapse
 
kosich profile image
Kostia Palchyk • Edited

Ah, yes, it is similar to Vue's computed! It was one of the inspirations for this lib and we had a hope it would look familiar to Vue devs!

And with Observable streams you get additional features. For example, if a value is not produced synchronously — autorun would halt execution until it does. Exploiting this feature we can track never emitting stream to get a filtering behavior:

const a = timer(0, 1_000);
const c = computed(() => $(a) % 2 ? $(NEVER) : $(a) + " - even");
// > 0 - even > 2 - even > …
Enter fullscreen mode Exit fullscreen mode

NOTE: it's not undefined at 1, it's skipped!

↑ try this @ stackblitz.com/edit/rxjs-autorun-f...

Not to mention that you can pipe the resulting stream with the many Rx operators. And I wouldn't even start describing late subscription / early unsubscription flows — the subject is too big for a comment 🙂