简体   繁体   English

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

[英]functions that return promises must be async

When I run tslint on my code I get the following error 当我在我的代码上运行tslint时,我收到以下错误

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 ? 有什么建议么 ?

This error message is caused by the tslint rule promise-function-async . 此错误消息是由tslint规则promise-function-async引起的

You can adhere to this rule by adding async on your arrow function expression: 您可以通过在箭头函数表达式上添加async来遵守此规则:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM