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
To supplement @Brandon's answer, using first()
or the like is also essential for updating a BehaviorSubject
based on its Observable
. For example (untested):
var subject = new BehaviorSubject({1:'apple',2:'banana'});
var observable = subject.asObservable();
observable
.pipe(
first(), // <-- Ensures no stack overflow
flatMap(function(obj) {
obj[3] = 'pear';
return of(obj);
})
)
.subscribe(function(obj) {
subject.next(obj);
});
UPDATE(DEC/2021):
As toPromise()
function has been deprecated in RxJS 7, new functions have been announced to be used instead of it. firstValueFrom
and lastValueFrom
.
firsValueFrom
function resolves the first emitted value and directly unsubscribe from the resource. It rejects with an EmptyError when the Observable completes without emitting any value.
On the other hand, lastValueFrom
function is, to a certain degree, same to toPromise()
as it resolves the last value emitted when the observable completes. However, if the observable doesn't emit any value, it will reject with an EmptyError. Unlike toPromise()
which resolve undefined
when no value emits.
For more information, please check the docs.
Old Answer:
If you want to call an Observable only one time, it means you are not going to wait for a stream from it. So using toPromise()
instead of subscribe()
would be enough in your case as toPromise()
doesn't need unsubscription.
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