【发布时间】:2020-08-17 23:45:09
【问题描述】:
我目前正在编写一个向第 3 方网站发送请求的脚本,但一段时间后 cookie 无效并抛出 403
所以我有我的脚本来发送请求,如果它抛出 403 然后我们发送 VALID cookie 然后重新发送请求。
但是,state.device_info 在 catch 函数中更新后似乎仍未定义?
(async () => {
let state = {
'access_denied': false,
'cookies': {}
};
try {
await getCookies(state);
state.device_info = await getInfo(state).catch(async (error) => {
let localError = handleError(error, state);
if(localError === 'access_denied') {
state.access_denied = true;
//now lets unlock the request & send it again!
let post = await sendValidCookies(state);
if(post.data.success === true) {
//update state.device_info with WORKING request!
state.device_info = await getInfo(state).catch(async (error) => {
console.log('Damn we got another error!');
console.log(error);
})
if(state.device_info.status === 200) {
console.log('We got our info info using the unlocked request!');
state.access_denied = false;
}
}
}
});
if(state.device_info === undefined || state.access_denied === true) {
console.log('We have an undefined value!');
console.log(state.device_info); //undefined
console.log(state.access_denied); //false
return false;
}
} catch(error) {
console.log('major error!');
}
})();
【问题讨论】:
-
尝试用 try/catch 包裹
await getInfo,而不是用 promise catch。我认为您下面的代码可能在错误处理之前执行。
标签: javascript