【问题标题】:define another function within an ASYNC function in Angular在 Angular 的 ASYNC 函数中定义另一个函数
【发布时间】:2021-03-03 02:34:14
【问题描述】:
如何在 Angular 的 ASYNC 函数中定义另一个函数?我想调用一个函数,然后执行承诺中的内容。我不知道如何在异步中进行回调。它也不允许我在另一个嵌套函数中调用异步函数
【问题讨论】:
标签:
angular
typescript
asynchronous
ionic-framework
async-await
【解决方案1】:
使用 await 你可以调用另一个返回 promise 的方法
async getAsyncData() {
this.asyncResult = await this.httpClient.get<Employee>(this.url).toPromise();
console.log('No issues, I will wait until promise is resolved..');
}
【解决方案2】:
在 Angular 中调用 ASYNC 函数是这样的:
async function getNames(){
console.log('Hello this is an example');
}
async function test(){
const response = await getNames();
console.log(response);
}
在调用之前你必须写 await。