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 fromFetch
, 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
1 Answer
Reset to default 8This 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())