Home > OS >  How to use async/await on event handlers on buttons in JavaScript?
How to use async/await on event handlers on buttons in JavaScript?

Time:07-30

I'm new too JavaScript and am having trouble with async. I have a webpage with two buttons, If the yes button is pressed it will do some code and if the no button is pressed it will do different code but regardless of which one is pressed it will continue on in the function. For Example

function mainloop(){

        document.getElementById("buttonyes").addEventListener("click", () => {
            /* do some code */
        })
        
        document.getElementById("buttonno").addEventListener("click", () => {
            /* do some different code */
        })
        /* wait for either button to be pressed and then continue with code */
        console.log("yay waiting for stuff")
}

I believe the solution to this is promises and creating other functions to handle the buttons; however, tutorials I've seen only show one function solutions and if I understand correctly the EventListener when activated is using another function all to itself. I've come over from C and all this object properties asynchronization stuff is throwing me for a loop. Any help would be much appreciated and downvoting me into a deep depression is allowed.

  • Related