0

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.

MyGiG
  • 1
  • 1
  • Waiting with a busy loop is a no-go. Never do that. It is doesn't work when waiting for a promise event, or anything else that comes in via the event loop. And it is blocking. It only makes sense to use `await` with an expression that evaluates to a promise. Don't use global variables for this. Make your `async` function `return` the value of interest. The caller should then again await the resolution of the promise returned by the function. Forget synchronous access to this. Embrace the asynchronous coding style. See reference. – trincot Jun 09 '21 at 07:06
  • I believe I understand the difference between async and sync well enought, but I can't figure out how to solve my problem. Or is it just not possible the way I set it up? – MyGiG Jun 09 '21 at 07:16
  • @Revno Thank you for the suggestion. I tried your example [link](https://jsfiddle.net/42wnuz7p/), but it sadly doesn't work as google.script.run... returns the promise immidietly, this is the reason I'm stuck. – MyGiG Jun 09 '21 at 07:29
  • For example, is this information useful for your situation? https://github.com/tanaikech/syncGoogleScriptRun – Tanaike Jun 09 '21 at 08:19
  • When you use promises, then introducing a function like `waitForAnswer` shows that you haven't really understood how promises work. There should be no code that "polls" for a result to come in. Use `await` on the promise you get back from the google API. And read the linked Q&A at the top, which answers a multitude of questions and misunderstandings about getting a result from an asynchronous API (like the one you use). – trincot Jun 09 '21 at 08:46

0 Answers0