首页 > 解决方案 > functions that return promises must be async

问题描述

When I run tslint on my code I get the following error

functions that return promises must be async

Here is the code

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

Not sure now how to fix this because the code runs just fine! Any suggestions ?

标签: javascriptasynchronouspromiseasync-awaittslint

解决方案


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

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

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


推荐阅读