调用wait-fetch步骤超出了我的函数,并且没有完成其余的代码



我有一个函数来加载一个JSON文件,其中包含如下信息:

async function loadJsonFile() {
try{
const jsonResponse = await fetch("/config/jsonfile.json", {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
});
return await jsonResponse.json()
} catch(e){
console.log(e.message);
return e.message;
}
}

然而,在调试我的代码时,loadJsonFile被介入。然后用await fetch点击代码行,然后退出函数,之后没有完成任何其他操作。我想知道是否有人能提供帮助?

export const getInit = () => {
return async (dispatch) => {
const json = await loadJsonFile();
// more code that needs to be completed below...
}
}

更新1

我注意到我的捕获声明中有一个拼写错误。我已经解决了这个问题,现在仍然遇到同样的问题。

你认为用不同的语法做这件事怎么样,比如:

function loadJsonFile() {
return fetch('/config/jsonfile.json', {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
})
.then(response => response.json())
.catch(error => {
console.log(error.message);
return error.message;
});
}

这可能是因为您的catch块,您将err作为错误变量,但您使用了console.log(e.message); return e.message;,并且由于eundefined,因此会导致未处理的错误累积器。你可以这样更改:

...
catch(err){
console.log(err.message);
return err.message;
}
...

我认为您应该尝试删除第二个await

最新更新