Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

286
Views
Undefined variables called from a function

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
    }
    
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!