返回承诺的函数必须是异步的

Jea*_*eri 2 javascript asynchronous promise async-await tslint

当我在我的代码上运行 tslint 时,我收到以下错误

functions that return promises must be async
Run Code Online (Sandbox Code Playgroud)

这是代码

private doSomething(): void {
    fetch(myUrl)
        .then((rsp: Response) => rsp.text()) // <-- gives error
        .then(txt => this.txt = txt);
}
Run Code Online (Sandbox Code Playgroud)

现在不知道如何解决这个问题,因为代码运行得很好!有什么建议 ?

jor*_*ake 6

此错误消息是由 tslint 规则promise-function-async 引起的

您可以通过在箭头函数表达式上添加 async 来遵守此规则:

.then(async (rsp: Response) => rsp.text())

  • 删除 async 和左括号之间的空格。您还可以配置编辑器/linter 以自动修复其中一些错误。 (2认同)