Async function as event handler

Hi there,
is it possible to use promises inside events handlers using async await?
I thought it was possiible, but now i’m facing some strange behaviour and i’m not so sure.
This is a snippet of code:

           let b = new BABYLON.GUI.Button.CreateSimpleButton("generalPurpose","Conferma e continua\ncon altri articoli");            
            b.onPointerDownObservable.add((**async** function(){   
                this.currentAlert.dispose();
                this.currentAlert = null;                       
                if(generalPurpFunction!==null){
                    // esegue l'eventuale callback
                    let qta = 0;
                    let um = "-";
                    if(inputBox!==null){
                        qta = parseFloat(inputBox.text);
                        um = data._arum1;
                    }                                 
                    **await** generalPurpFunction(qta,um);
                }
            }).bind(this));

generalPurpFunction is my asyncronous function.
In DOM it could be working, does Babylon works the same?

Yup this would work the same but as the observable is not awaiting your returned promise, it will be hard to deal with any timing you d like for generalPurpFunction basically you ll end up in a fire and forget mode.

1 Like

Thank you @sebavan,
so should i implement inside my generalPurpFunction some strategy to freeze the scene ( or something similar) to block the user till the work is done?

If it needs to be blocked, yes or all the rest will carry on as normal otherwise.

2 Likes