To use await with Rxjs observables, we've to convert it to a promise first. To do that, we can use the firstValueFrom or lastValueFrom functions. firstValueFrom returns promise that resolves to the first value of an observable. lastValueFrom returns promise that resolves to the last value of an observable.
You can use Observables with Promises and with async/await to benefit from the strengths of each of those tools.
Using Async/Await in Angular One of the best improvements in JavaScript is the Async/Await feature introduced in the ECMAScript 7. Basically, Async/Await works on top of Promise and allows you to write async code in a synchronous manner. It simplifies the code and makes the flow and logic more understandable.
A common misconception in Angular development is regarding whether observables are synchronous or asynchronous. A lot of (even experienced Angular developers) think that observables are async, but the truth is that they can be… Both synchronous and asynchronous.
You have to pass a promise to await
. Convert the observable's next event to a promise and await that.
if (condition) {
await observable.first().toPromise();
}
Edit note: This answer originally used .take(1) but was changed to use .first() which avoids the issue of the Promise never resolving if the stream ends before a value comes through.
As of RxJS v8, toPromise
will be removed. Instead, the above can be replaced with await firstValueFrom(observable)
Use the new firstValueFrom()
or lastValueFrom()
instead of toPromise()
, which as pointed out here, is deprecated starting in RxJS 7, and will be removed in RxJS 8.
import { firstValueFrom} from 'rxjs';
import { lastValueFrom } from 'rxjs';
this.myProp = await firstValueFrom(myObservable$);
this.myProp = await lastValueFrom(myObservable$);
This is available in RxJS 7+
See: https://indepth.dev/rxjs-heads-up-topromise-is-being-deprecated/
It likely has to be
await observable.first().toPromise();
As it was noted in comments before, there is substantial difference between take(1)
and first()
operators when there is empty completed observable.
Observable.empty().first().toPromise()
will result in rejection with EmptyError
that can be handled accordingly, because there really was no value.
And Observable.empty().take(1).toPromise()
will result in resolution with undefined
value.
You will need to await
a promise, so you will want to use toPromise()
. See this for more details on toPromise()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With