Any need to call unsubscribe for RxJS first()

瘦欲@ 提交于 2019-12-08 18:48:20

问题


In the following code:-

RxJS.Observable.of(1,2).first().subscribe((x) => console.log(x););

is it necessary to unsubscribe given the operator first()?


回答1:


No. It unsubscribes automatically after calling first(). The current syntax is observable.pipe(first()).subscribe(func); for RxJS 6.




回答2:


For the example provided, you dont need to unsubscribe, and neither you need to call first, as Observable.of(1) actually completes after emitting its first (and last) value.




回答3:


first() will complete after the first item is emitted from the observable.

Also subscribe() takes three arguments, the last one being the complete callback. Running the following code will output 1 followed by 'done'.

Rx.Observable.of(1)
  .subscribe(
  (x) => console.log(x),    // next
  (x) => console.error(x),  // error
  () => console.log('done') // done
)


来源:https://stackoverflow.com/questions/49683600/any-need-to-call-unsubscribe-for-rxjs-first

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!