Is there a way to make my code work in the logical order described in the comments?
var globalVariable;
    async function function1(){
        let sqlTableName = "work_orders"
        //Async call to google app script, which functions as backend
        //It is basically: promise.then(callback)
        google.script.run.withSuccessHandler(databaseReturn).getDatabaseTableContent(sqlTableName);
        //Synchronous call to wait for globalVariable to change to 1
        await waitForAnswer();
        
        //Some code to be extecuted after globalVariable is equal to 1
        console.log("globalVariable: ", globalVariable)
    }
    function waitForAnswer(){
        //Some sort of loop to check if variable equals to 1
        while (globalVariable != 1){
            //Maybe a timeout function
            //Or something else
            console.log("waiting...");
        }
        return;
    }
    function databaseReturn(dataFromDatabse){
        console.log(dataFromDatabse);
        globalVariable = 1;
        return;
    }
    function1();
I've tried, on and off for weeks, various async/awaits and promises with setTimeout setTimeout(() => {console.log("timeout 200ms");}, 100); to loop in waitForAnswer(). But something is wrong with my logic/flow or there is some knowledge missing in my head. Either the browser crashes or the execute orders is not what I need it to be.
I need function1() to work in that order, waitForAnswer() can be modified at will.
