简体   繁体   中英

Rx.js unsuscribe giving gerror

I'm currently starting out with observables in JS, and I've hit a total roadblock with what seems like something really basic. I've created the super simple example below, which whilst useless by itself, looks to me like it should work, but it doesn't. the final line throws an error.

const obs1 = Rx.Observable.timer(2000)
    .map(() => {return Math.random() > 0.5});

const subscription = obs1.subscribe(
   (v) => {
      console.log('hello world: ', v)
   });

subscription.unsubscribe();

The error is

TypeError: subscription.unsubscribe is not a function

My question is basically, why can't I unsubscribe from the above observer? Or how should I unsubscribe from it?

You might be using Rx v4.x, isn't it? In that case, the method is dispose not unsubscribe

subscription.dispose();

Your two options are to either:

  • upgrade to rxjs v5;
  • replace unsubscribe with dispose .

If you codebase hasn't mush code yet, I suggest you upgrade to v5 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM