return 承诺的函数必须是异步的

functions that return promises must be async

当我 运行 对我的代码执行 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);
}

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

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

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

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