MessageBox - Force it to be blocking

141 Views Asked by At

In order to keep a clean, easy and maintainable code, I want to exit a function, after the user has closed a MessageBox.... BUT the anonymous function is triggered async, therefore not blocking. Is there a best practice to achieve this? I always was in love with c#'s MessageBox implementation.... so I expected this in UI5, too. So sad. Here the code...

                    } else { } */
                
                MsgBox.error(info,                  
                    {
                    actions: [MsgBox.Action.OK], emphasizedAction: MsgBox.Action.NO,
                                onClose: function (sAction) 
                                {
                                    abort = true;                 
                                }
                });
            }

            if(abort){

                return;
            }

            this.clearLocalModel();
            // code
            // more code
            // which is executed , but should not
            // I KNOW, i can redesign this with if else... and so on- BUT
            // i would like to keep it pretty simple...

UPDATE:

I tried the idea with the promise, returning a bool. As "promised" I returned for feedback, and I am in the same situation like before... in this case I added screenshots, I hope, You don't mind...

Caller:

enter image description here

Callee:

enter image description here

1

There are 1 best solutions below

5
Marc On

On a very high level you could create something like the following function. It presents a MessageBox and depending on what the user clicks resolves or rejects a promise.

askUserForToContinue: function() {
    return new Promise((resolve, reject) => {
        MessageBox.error(info, {
            actions: [MsgBox.Action.OK],
            onClose: (sAction) => {
                if (sAction === MsgBox.Action.OK) {
                    // return true, flow should continue
                    resolve(true);
                } else {
                    // return false, flow should continue
                    resolve(false);
                }
            }
        });
     
    }
}

Then from somewhere else you can call this function and wait for the result (using then or async/await which both will feel more like a synchronous flow).

handleFlow: async function() {
    if (await this.askUserForToContinue()) {
        // Promise was resolved with true, do stuff (ideally call another method)
    } else {
        // Promise was resolved with false, do other stuff (ideally call another method)
    }
}