Why is this happening?
As mentioned here, these are the main reasons why toPromise is being deprecated:
One goal was to remove it from the
Observableprototype and turn it into a standalone util function.The naming of
toPromiseis not the best. Especially when used in combination withawaitit does not read very well:await categories$.toPromise()vsawait lastValueFrom(categories$)The type information of
toPromiseis wrong. When the sourceObservablecompleted without ever emitting a single value – it
resolved withundefined. It should reject in that case. APromiseis a
“promise” that when it resolves a value will be there – and be it
undefined. But when the stream completes without ever emitting a value
you can’t differentiate between a stream that a emittedundefinedon
purpose and a stream that completed without ever emitting anymore
What should you use next?
If you really insist doing it the promise way, lastValueFrom/firstValueFrom. Otherwise switching to reactive programming would be the way to go.
Using toPromise ( deprecated ) –
public async loadCategories() {
this.categories = await this.inventoryService
.getCategories()
.toPromise()
}
Using lastValueFrom ( new ) –
import { lastValueFrom } from 'rxjs';
public async loadCategories() {
const categories$ = this.inventoryService.getCategories();
this.categories = await lastValueFrom(categories$);
}
This link should help –
https://indepth.dev/posts/1287/rxjs-heads-up-topromise-is-being-deprecated