首页 > 解决方案 > 什么是异步 void 的 typedef

问题描述

与这个问题有点相关,但有所不同。

TSLint 抱怨这段代码,因为它需要一个 typedef:

  private async getTranslations() {
    // this.translations is a public variable used by the html
    this.translations = await this._languageService.getTranslations('Foo');
  }

我将其更新为

private async getTranslations() : void { ... }

这给了我错误:

type 'void' 在 ES5 中不是有效的异步函数返回,因为它不引用 pPromise 兼容的构造函数值

在不删除 async 关键字的情况下如何才能做到这一点?

标签: javascriptangulartypescriptasynchronous

解决方案


将返回类型更改为:

private async getTranslations(): Promise<void> {
  this.translations = await this._languageService.getTranslations('Foo');
}

推荐阅读