返回promise的函数必须是异步的

时间:2018-11-22 16:04:58

标签: javascript asynchronous promise async-await tslint

在代码上运行tslint时,出现以下错误

functions that return promises must be async

这是代码

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

现在不确定如何解决此问题,因为代码可以正常运行!有什么建议吗?

1 个答案:

答案 0 :(得分:2)

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

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

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