【问题标题】:Update state inside catch in javascript在javascript中更新catch中的状态
【发布时间】: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


【解决方案1】:

不要在catch 中定义它,而是返回值。这个返回值将被赋值给state.device_info

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) {
                //Get result from WORKING request
                const result = 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;
                }

                return result;
            }
        }
});

【讨论】:

    【解决方案2】:

    由于您在方法调用 getInfo(state) 前面使用了 await 关键字,并且在之后调用 .catch(...),我假设 getInfo(state) 返回一个 Promise。

    在 JavaScript 中有两种处理 Promise 的方法:

    • 通过显式调用.then(result => {...}),同时使用.catch(error => {...}) 捕获错误。
    • 通过将await 关键字放在您的方法调用前面。通过使用这种语法,如果你想捕获错误(你应该这样做),你需要使用try { } catch (error) { } 语法。

    我从未像您那样混合使用这两种方法,我认为这可能是您遇到问题的根源。

    对于您的情况,它应该如下所示:

    try {
      state.device_info = await getInfo(state);
    } catch (error) {
      // Your logic here...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 2019-01-19
      • 1970-01-01
      • 2023-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多