我在 referencesPromise
和 contactTypesPromise
$onInit()
中的两个变量中分配了对 Web 服务的两次调用(我可以创建一个新方法为此,如果需要的话)
$onInit() {
const referencesPromise = this.ReferenceService.getMultipleReferences(this.AgentReferences)
const contactTypesPromise = this.ContactService.getContactTypes()
Promise.all([referencesPromise, contactTypesPromise]).then((responses) => {
this.references = responses[0]
this.contactTypes = responses[1]
const stateParams = this.$state.params
return this.setContactSteps()
})
}
他对 async-await 的替代方案是什么?
最佳答案
假设您仍然希望您的方法同时运行,则无需进行太多更改:
async $onInit() {
const referencesPromise = this.ReferenceService.getMultipleReferences(this.AgentReferences);
const contactTypesPromise = this.ContactService.getContactTypes();
this.references = await referencesPromise;
this.contactTypes = await contactTypesPromise;
const stateParams = this.$state.params;
return this.setContactSteps();
}
请注意初始调用是如何相同的,我们仍然希望捕获 promise ,因为我们希望两个请求同时运行。
关于JavaScript/Angular 1 - Promise.all 到异步等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48318869/