我有一個函式來加載一個 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
我注意到我的 catch 陳述句中有一個錯字。我已經解決了這個問題,并且仍然遇到同樣的問題。
uj5u.com熱心網友回復:
這是因為你的catch塊,也許,你err的錯誤變數,但你已經使用console.log(e.message); return e.message;,并因為e是undefined它會導致未處理的錯誤accurs。你可以像這樣改變它:
...
catch(err){
console.log(err.message);
return err.message;
}
...
uj5u.com熱心網友回復:
您如何看待使用不同的語法進行操作,例如:
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;
});
}
uj5u.com熱心網友回復:
我認為您應該嘗試洗掉第二個await.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/375496.html
標籤:javascript 异步等待 反应还原
上一篇:將引數傳遞給與Playwright的評估一起使用的內部箭頭函式
下一篇:如何獲取部分類名的所有實體
