MalwareMoon
MalwareMoon

Reputation: 886

ESLint + TypeScript ESlint; How to make a function returning a promise

I have ESLint + TypeScript ESlint which no rules in them changed in the project. In that project is a function, where I want to return a promise which is axios.get call, but I am unable to get it to pass linting rules.

When I do:

import { ResponseType } from 'axios'

const getPosts = async (): Promise<ResponseType> => {
  return axios.get("https://jsonplaceholder.typicode.com/posts")
}

I get:

Returning an awaited promise is required in this context. eslint(@typescript-eslint/return-await)

So, I added await:

import { ResponseType } from 'axios'

const getPosts = async (): Promise<ResponseType> => {
  return await axios.get("https://jsonplaceholder.typicode.com/posts")
}

But this lead to another error:

Redundant use of await on a return value. eslint(no-return-await)

I also tried to get rid of async, but that lead to:

Functions that return promises must be async. eslint(@typescript-eslint/promise-function-async)

I doubt that default ESLint conflicts with TypeScript ESlint and assume, that there is some other solution to rewrite the function, so that it passes for both. What would be some other option to rewrite that function?

Here documentation for both:

https://eslint.org/docs/latest/rules/no-return-await

https://typescript-eslint.io/rules/return-await

Upvotes: 1

Views: 1230

Answers (1)

Dimava
Dimava

Reputation: 10851

General principle:

If some JS eslint rule conflicts with TS rule, disable JS rule

In many cases TS rule may conflict with JS rule with same name

In your case, your rule conflicts with opposite rule which should have been disabled
Find out why it gets enabled, or just disable is and forget

Upvotes: 1

Related Questions