Q) How do I convert the following observable to a promise so I can call it with .then(...)
?
My method I want to convert to a promise:
toPromise is deprecated in RxJS 7.
Use:
lastValueFrom
Used when we are interested in the stream of values. Works like the former toPromise
Example
public async getAssetTypes() {
const assetTypes$ = this._APIService.getAssetTypes()
this.assetTypes = await lastValueFrom(assetTypes$);
}
firstValueFrom
Used when we are not interested in the stream of values but just the first value and then unsubscribe from the stream
public async getAssetTypes() {
const assetTypes$ = this._APIService.getAssetTypes()
this.assetTypes = await firstValueFrom(assetTypes$); // get first value and unsubscribe
}