【问题标题】:ESLint + TypeScript ESlint; How to make a function returning a promiseESLint + TypeScript ESlint;如何使函数返回承诺
【发布时间】:2022-12-11 08:15:47
【问题描述】:

我有 ESLint + TypeScript ESlint,它们中的规则在项目中没有改变。在那个项目中有一个函数,我想在其中返回一个 axios.get 调用的承诺,但我无法让它通过 linting 规则。

当我做:

import { ResponseType } from 'axios'

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

我得到:

在这种情况下需要返回等待的承诺。 eslint(@typescript-eslint/return-await)

所以,我添加了等待:

import { ResponseType } from 'axios'

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

但这会导致另一个错误:

在返回值上重复使用 await。 eslint(不返回等待)

我也试图摆脱异步,但这会导致:

返回承诺的函数必须是异步的。 eslint(@typescript-eslint/promise-function-async)

我怀疑默认 ESLint 与 TypeScript ESlint 冲突,并假设有一些其他解决方案可以重写该函数,以便它通过两者。重写该功能的其他选择是什么?

两者的文档如下:

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

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

【问题讨论】:

    标签: typescript promise eslint


    【解决方案1】:

    一般原则:

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

    在许多情况下,TS 规则可能与同名的 JS 规则冲突

    在您的情况下,您的规则与对面的应该被禁用的规则
    找出它被启用的原因,或者只是禁用它并忘记

    【讨论】:

      猜你喜欢
      • 2023-02-03
      • 1970-01-01
      • 2020-04-26
      • 2016-06-22
      • 2021-11-19
      • 2021-01-08
      • 2023-03-14
      • 2020-11-30
      • 2019-07-30
      相关资源
      最近更新 更多