Reputation: 341
Could anyone shed some light on how I would immediately unsubscribe from an RxJS
subscription? The observable is from Angular 2 EventEmitter
. When I recieve the event, I want to cancel the subscription. The issue here is cancelling the subscription within the function block. I have a feeling that this is the wrong approach:
this.subscription = observable.subscribe(result => {
// do something
// **unsubscribe**
});
Upvotes: 28
Views: 10074
Reputation: 657068
observable.first().subscribe(....)
will end the subscription after the first event.
Update for RxJs 6+
observable.pipe(first()).subscribe(....)
Upvotes: 59