Javascript 将同步函数修改为角度中的异步函数

Javascript 将同步函数修改为角度中的异步函数,javascript,angular,async-await,Javascript,Angular,Async Await,我正在尝试将此函数修改为async/await。我怎么做 public getProduct(id: number){ this.subscriptions.add( this.productService.getProduct(id).subscribe(((res) => { return res }), error => { console.log(error);

我正在尝试将此函数修改为async/await。我怎么做

  public getProduct(id: number){
      this.subscriptions.add(
        this.productService.getProduct(id).subscribe(((res) => {
          return res
        }),
        error => {
          console.log(error);
        })
      );
    
  }

如果您特别需要使用异步,请等待;我建议采取以下措施:

// Warning, probable anti-pattern below
async getProduct(id: number)) {
    const result = await this.productService.getProduct(id).toPromise();

}

如果使用this.subscriptions.add()方法,则不需要使用异步/等待函数。您可以随时使用this.subscription.subscripte()。