【问题标题】: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。

    【讨论】:

      猜你喜欢
      • 2011-12-11
      • 1970-01-01
      • 1970-01-01
      • 2012-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      相关资源
      最近更新 更多