我一直在试图弄清楚为什么异步获取请求在我的reactjs应用程序中被完全忽略。基本上,我调用Integrate(),如果从其内部调用生成成功的状态结果,则等待辅助异步常量调用fetchOtherStuff()的结果
Integrate 执行时没有出现问题,但由于某种原因,Chrome 完全忽略了 fetchOtherStuff 中的提取请求。
我知道它正在被调用,正如
THIS WILL LOG
每次记录的那样。但是,没有发出网络请求的记录,并且可以预见,THIS WILL NOT LOG
不会记录。
没有控制台错误,没有网络请求,几乎就像它只是中止调用(以及下面的任何其他等待的常量
fetchOtherStuff()
有人遇到过这种情况吗?我在 S.O. 上没有看到任何内容
(在某处打电话给
await integrate()
)
const integrate = async () => {
const URL = 'https://whatever-endpoint.com';
try {
const response = await fetch(URL, {
method: "GET",
headers: {
"Content-Type": "application/json"
},
credentials: 'include'
});
const responseData = await response.json();
if (response.status === 200) {
await fetchOtherStuff();
}
} catch (error) {
return {status:500, body: 'Server Error, Please Try Again'}
}
}
const fetchOtherStuff = async () =>{
return new Promise(async (resolve,reject)=>{
try {
console.log("THIS WILL LOG")
const orgCodes = await fetch(`https://endpoint-whatever.com`, {
method: "GET",
headers: {
"Content-Type": "application/json"
},
credentials: 'include'
});
const result = await orgCodes.json();
console.log("THIS WILL NOT LOG ", result)
setDomainNotReadyCodes(result)
resolve(result)
} catch (error) {
reject(error)
}
})
}
确保在Integrate()中正确处理fetchOtherStuff()返回的promise。如果 fetchOtherStuff() 中存在未处理的拒绝,则可能会导致该函数的出现被忽略。确保捕获任何错误并记录它们
await fetchOtherStuff().catch(error => console.error('Error fetching other stuff:', error));