Home > Net >  Undefined variables called from a function
Undefined variables called from a function

Time:06-16

I have 2 functions that are called inside generateActivity, it returns a promise however when i console.log them it says undefined. I believe it's because of the way getSessionUser and getSessionTimer functions are made.

function getSessionUser() {
    win.webContents.executeJavaScript('sessionStorage.getItem("user");', true)
    .then(result => {
        if(result) {
           return JSON.parse(result);
        }
    });
}

function getSessionTimer() {
    win.webContents.executeJavaScript('sessionStorage.getItem("timer");', true)
    .then(result => {
        if(result) {
           return JSON.parse(result);
        }
    });
}

function generateActivity() {

    var user = getSessionUser();
    var timer = getSessionTimer();

    console.log(user); // undefined but should not be
    console.log(timer); // undefined but should not be

    if(user && timer) {

        activity.user = user;
        activity.timer = timer;

        var data = {
            activity: activity
        }

        axios.post("http://creaz:81/xorix/api/desktop/save_activity", data, {
            headers: headers
        })
        .then((response) => {
            //console.log(response);
        })
        .catch((error) => {
            //console.log(error);
        })

        activity.is_mouse = activity.is_keyboard = 0;

    } else {
        console.log('not logged in or no timer is running');
        // says undefined for both user and timer variable
    }
    
}

CodePudding user response:

I think you can use async/await for this. Make generateActivity async:

async function generateActivity() {
   …
}

And then await for the functions to return their results:

const user = await getSessionUser();
const timer = await getSessionTimer();

Hope this helps.

CodePudding user response:

Your getSessionUser() and getSessionTimer() also need to return a promise. Both of these functions are asynchronous functions, but your code inside generateActivity treat them as synchronous functions. You need to wait for the promise to resolve before you can console.log the result.

function getSessionUser() {
    return win.webContents.executeJavaScript('sessionStorage.getItem("user");', true)
    .then(result => {
        if(result) {
           return JSON.parse(result);
        }
    });
}

function getSessionTimer() {
    return win.webContents.executeJavaScript('sessionStorage.getItem("timer");', true)
    .then(result => {
        if(result) {
           return JSON.parse(result);
        }
    });
}

Then, inside generateActivity(), you need to to wait for the promise to resolve. We can use the async await keywords to make this easier.

async function generateActivity() {

    var user = await getSessionUser();
    var timer = await getSessionTimer();
    // Now we wait for the async calls to resolve before continuing the execution inside this function.
}

Since getSessionUser and getSessionTimer are independent, we can improve the above code snippet by executing both functions at the same time, but wait for both promises to resolve before we continue the rest of the code execution. (I'll leave this as an exercise/challenge for OP)

  • Related