最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - functions that return promises must be async - Stack Overflow

programmeradmin1浏览0评论

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 ?

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 ?

Share Improve this question asked Nov 22, 2018 at 16:04 Jeanluca ScaljeriJeanluca Scaljeri 29.2k66 gold badges233 silver badges380 bronze badges 4
  • 1 @Kraylog no...that's not how fetch() API works. rsp.text() returns promise – charlietfl Commented Nov 22, 2018 at 16:12
  • 1 rsp is the response from Fetch, so I don't think it can be synchronous – Jeanluca Scaljeri Commented Nov 22, 2018 at 16:13
  • 1 response.text() returns a promise. developer.mozilla/en-US/docs/Web/API/Body/text; – jordrake Commented Nov 22, 2018 at 16:13
  • Are you using the correct Response type? – dotconnor Commented Nov 22, 2018 at 16:15
Add a ment  | 

1 Answer 1

Reset to default 8

This error message is caused by the tslint rule promise-function-async.

You can adhere to this rule by adding async on your arrow function expression:

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

发布评论

评论列表(0)

  1. 暂无评论