Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript “声纳林”;重构redunant等待非承诺”;代码提示。。。但函数确实返回了一个承诺_Javascript_Visual Studio Code_Promise_Jsdoc_Sonarlint - Fatal编程技术网

Javascript “声纳林”;重构redunant等待非承诺”;代码提示。。。但函数确实返回了一个承诺

Javascript “声纳林”;重构redunant等待非承诺”;代码提示。。。但函数确实返回了一个承诺,javascript,visual-studio-code,promise,jsdoc,sonarlint,Javascript,Visual Studio Code,Promise,Jsdoc,Sonarlint,我正在编写一个小脚本,为了调用方法(main)的可读性,我决定将带有一堆头的回迁封装到它们自己的方法中 async function main(){ ... let res = get_fromSpecificFetch(); ... } async function get_fromSpecificFetch() { try { return fetch("https://www.website.com/apiendpoint", {

我正在编写一个小脚本,为了调用方法(main)的可读性,我决定将带有一堆头的回迁封装到它们自己的方法中

async function main(){
...
   let res = get_fromSpecificFetch();
...
}

async function get_fromSpecificFetch() {
    try {
        return fetch("https://www.website.com/apiendpoint", {
            "headers": {
                "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
                "accept-language": "en-US,en;q=0.9",
                "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Google Chrome\";v=\"90\"",
                "sec-ch-ua-mobile": "?0",
                "upgrade-insecure-requests": "1"
            },
            "referrer": "https://www.website.com/",
            "referrerPolicy": "strict-origin-when-cross-origin",
            "body": null,
            "method": "GET",
            "mode": "cors"
        })
    } catch {
        throw new Error("launch");
    }
}

Sonarint突出显示了我的代码并说: “重构此冗余文件,等待非承诺”

…但这确实是一个承诺。如果我不等待这一刻,那么我的承诺仍将悬而未决。我不想禁用这个规则(因为它正确时很有用),但在这种情况下它是错误的

我知道我没有使用typescript,那么solarlint怎么知道返回类型呢? 用JsDoc之类的东西定义返回类型可以解决这个问题吗

也许有更好的方法来组织我的代码


(我不能一次定义此提取包装并传递不同的url/headers对象,因为有时在我的提取包装内会有解析逻辑,但这无论如何也不能解决我的问题)

如果从
get\u fromSpecificFetch
中删除
async
,从其中删除
try/catch
块,会发生什么情况,或者完全省略
catch
块,或者在
fetch
promise上将其转换为
.catch()
方法调用?我最终将抓取放在不同的文件中(无论如何对组织更有利),导入了它们,提示消失了