简体   繁体   English

实现 async/await angular 7

[英]Implement async/await angular 7

Can someone please help me implement async/await for this angular/typescript method有人可以帮我为这个角度/打字稿方法实现异步/等待吗

 onCustomerSelected(cId) { let cust = this.custlist.find((c) => c.custId == cId); this.service.setCustomer(cust); //need to await this method (note, this doesnt return promise) ... do some processing ... (note, i need to wait for the service to set customer first, before i can begin further processing) }

Assuming this.service.setCustomer returns a Promise , you just need to flag the function onCustomerSelected as async and then await this.serice.setCustomer假设this.service.setCustomer返回一个Promise ,您只需要将onCustomerSelected函数onCustomerSelectedasync ,然后await this.serice.setCustomer

async onCustomerSelected(cId) {
    let cust = this.custlist.find((c) => c.custId == cId);

    await this.service.setCustomer(cust); //need to await this method (note, this doesnt return promise)

}

If this.service.setCustomer is an Observable , then you need to do await this.service.setCustomer(...).toPromise();如果this.service.setCustomer是一个Observable ,那么你需要做await this.service.setCustomer(...).toPromise();

您需要将您的服务调用包装在一个 Promise 中并使用 await。

const result = await this.service.setCustomer(cust).toPromise()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM