Change function return to synchronous

Asked

Viewed 109 times

2

Hello, I need to modify this function to use callback as a function return and not as a new function call via callback.

I need it this way because I am creating a generic method for exclusion confirmations in the existing system. If I don’t succeed like this, it will generate me a lot of changes in the application.

I am using the JS Bootbox library and the method is:

function Confirm(messageOfConfirmation){
    bootbox.confirm({
        message: messageOfConfirmation,
        buttons: {
            confirm: {
                label: 'Yes',
                className: 'btn-success'
            },
            cancel: {
                label: 'No',
                className: 'btn-danger'
            }
        },
        callback: function (result) {
            functionOfCallback(result);
        }
    });
}

And to reinforce and avoid understanding of duplicity in the question, follow an example of how I intend to use.

function PodeExcluir(){
     if(Confirm("Tem certeza que deseja excluir?"){
        //Chamada de exclusão aqui
    }
}

I mean, today the system already uses the JS standard confirm but need to change.

1 answer

2


A simple way to solve this is to use your own callback.

Example:

function Confirm(messageOfConfirmation, functionOfCallback) {
    //nenhum alteração necessária
}

function PodeExcluir(){
     Confirm("Tem certeza que deseja excluir?", function(result) {
         //Chamada de exclusão aqui
     });
}

Understand that, as Javascript code is interpreted linearly in the browser, the use of elements for user interaction - with the exception of the standard confirmation box - must be asynchronous, that is, using, callbacks/*listeners, events, etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.