'Promise returns undefined value

I need to retrieve the result of my Api call outside the promise but the value I get is undefined.

In OrderService :

public async getOrderPrice(device: string) : Promise<any> {
    this.urlOrderPrice = this.urlOrderPrice.replace("device", device);
    return await this.http.get(this.urlOrderPrice).toPromise();
  }

In 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;
  }


Solution 1:[1]

in getOrder() when your id==null, then returned order is undefined

also remove extra await from rest call

public async getOrderPrice(device: string) : Promise<any> {
    this.urlOrderPrice = this.urlOrderPrice.replace("device", device);
    return this.http.get(this.urlOrderPrice).toPromise();
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Antoniossss