get variable from one function to second in javascript

I have a problem with getting variable from first function to second function. I have … Read more get variable from one function to second in javascript

I have a problem with getting variable from first function to second function. I have read a lot of links and answers from another questions like mine, but I couldn’t solve my problem.

So I have an async function, named getUser() with variable named id_res. And I want to get id_res to my second function.

var id = {};
async function getUser() {
    let user = document.getElementById('user').value;
    let passs = document.getElementById('pass').value;

    let res = await eel.login(user, passs)();

    let text = res[0];
    let id_res = res[1];

    if (text == 'Вы успешно авторизовались!') {
        window.location.href = 'index.html';
    } else if (text == 'Неправильное имя пользователя или пароль') {
        alert(text);
    }

    id["user"] = id_res; // that works correct 
    alert(id["user"]); // that works correct too
}

Now, I am going to show you my second function, which can’t get id_res variable.

async function createTask() {
    var user_id = id["user"]; // Here, id["user"] means "undefined". So that part of code doesn't work correct
    let task = document.getElementById('need_task').value;

    let result = await eel.create_task(user_id, task);
    alert(user_id);
}

As you can notice, I use async functions. So, I guess that my problem is with them.
I will appreciate any of your help. Thanks!

Source: JavaSript – Stack Overflow



Leave a Reply

Your email address will not be published. Required fields are marked *