I am facing an issue where I need to access the result of my API call outside the promise, but the value I receive is always undefined.
Within the OrderService :
public async getOrderPrice(device: string) : Promise<any> {
this.urlOrderPrice = this.urlOrderPrice.replace("device", device);
return await this.http.get(this.urlOrderPrice).toPromise();
}
Inside the OrderDetailsComponent :
public orderPrice: any;
ngOnInit(): void {
this.getOrder()?.then(data => { this.orderPrice = data; console.log(this.orderPrice) });
}
private async getOrder() {
const id = this.route.snapshot.paramMap.get('id');
let order;
if (id != null) {
order = this.orderService.getOrderPrice(id).then((result) => {
return result.results[Object.keys(result.results)[0]].val;
});
}
return order;
}