Create one-time subscription
Not 100% certain about what you need, but if you only want to observe the first value, then use either first()
or take(1)
:
observable.first().subscribe(func);
note: .take(1)
and .first()
both unsubscribe automatically when their condition is met
Update from RxJS 5.5+
From comment by Coderer.
import { first } from 'rxjs/operators'
observable
.pipe(first())
.subscribe(func);
Here's why
RxJS has some of the best documentation I've ever come across. Following the bellow link will take you to an exceedingly helpful table mapping use cases to operators. For instance, under the "I want to take the first value" use case are three operators: first
, firstOrDefault
, and sample
.
Note, if an observable sequence completes with no notifications, then the first
operator notifies subscribers with an error while the firstOrDefault
operator provides a default value to subscribers.
operator use case lookup