C
C
Coder3212017-10-04 00:10:39
JavaScript
Coder321, 2017-10-04 00:10:39

How to unsubscribe?

There is a function like this

const sub: Subscription = this.usersService.allUsers
 .subscribe(users=> {
otherBehaviorSubject.next(users.filter(/*some filter function*/));
sub.unsubscribe(); // В этом моменте постоянно матерится на то что sub === undefined
});

I only need to get a non-value from the BehaviorSubject once and immediately unsubscribe, how can I do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mykola, 2017-10-04
@IKMOL

Coder321 at least sub.unsubscribe(); put out of brackets

const sub: Subscription = this.usersService.allUsers
 .subscribe(users=> {
otherBehaviorSubject.next(users.filter(/*some filter function*/));
});
sub.unsubscribe();

K
KorsaR-ZN, 2017-10-12
@KorsaR-ZN

Use the operator takeit selects N elements from the sequence, completes it and unsubscribes

this.usersService.allUsers
 .take(1)
 .subscribe(users=> {
    otherBehaviorSubject.next(users.filter(/*some filter function*/));
 });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question