簡體   English   中英

其他 js 文件上的異步 function 總是返回 undefined,但是 function 運行

[英]Async function on other js files always return undefined, but the function run

我有一個異步 function 在 js 文件上返回 1 或 0 值,這是代碼

checkAuthentication: async function (client) {
        //search redis
        //get value key "token"
        try{
            var tempValue, tempErr

            client.get("token", async (err, value) => {
                tempValue = value
                tempErr = err
                if (tempErr) { // kalo gk ada di redis
                    console.log(tempErr) 
                    return 0
                }
                if (tempValue !== null) {//ADA DI REDIS
                    var start = new Date()
                    var responseRedis = JSON.parse(tempValue)
                    // console.log('this is old data' + responseRedis.accessToken)
    
                    var end = (new Date() - start)
                    console.info('Execution time: %d ms', end)
                    
                    jwt.verify(responseRedis.accessToken, accessTokenSecret, function (err, decoded) {//check if token expired or not
                        if (err) {//TOKEN EXPIRED
                            console.log(err)
                            return 0
                        } else {//TOKEN TIDAK EXPIRED
                            //tuker token jd refreshesh token
                            const newRefreshToken = jwt.sign({ username: decoded.user_name }, accessTokenSecret, { expiresIn: '24h' });
                            var data_send_redis = JSON.stringify({
                                "accessToken": newRefreshToken,
                            });
                            //store to redis 
                            // console.log('this is  new data' + data_send_redis)
                            client.setex("token", 600, data_send_redis);
                            return 1
                        }
                    });
                } else { //GK ADA DI REDIS TOKENYA
                    //LOG OUT
                    console.log('token not found')
                    return 0
                }
            })
        }catch (err){
            console.log(err)
            return 0
        }
    }

另一方面,我試圖在其他 js 文件中獲取名為 a 的變量中的值

var a = await model_auth_token.checkAuthentication(client)
console.log('hasil check auth : return ' + a)
if(a == 0){
    client.del('token', function(err, response) {
        if (response == 1) {
           console.log("Deleted Successfully!")
        } else{
         console.log("Fail to Delete")
         console.log(err)
        }
     });
     res.redirect('/panel/panel_login')
     return
}

每當我做控制台日志結果總是顯示未定義,我已經添加等待或然后在 function,但控制台日志結果總是顯示未定義

您從 a.then or.catch 回調返回,這是一個 function。

您需要將 client.get 塊包裝在 Promise 中:

return new Promise((resolve, reject) => { ... });

然后,使用resolve()來“成功”,並使用reject()來“失敗”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM