Make Callback run directly

Asked

Viewed 130 times

0

I took a Bootbox API just for the prompt, but the problem is that this syntax has a callback and with that, it only executes the code the second time I click on the button. Logically, I wanted it to run on the first click. (with the prompt normal in Javascript works as I want, but it’s horrible this prompt)

Follows the code:

function valida2(){
  bootbox.prompt({
    title: "This is a prompt with a number input!",
    inputType: 'number',
    callback: function (result) {
         Tips = parseInt(result);
         console.log(Tips);


    }
});
}


//a função que chama esse prompt
function ccck(){ 

 valida2();
 myArray[count] = [count + 1, 1 , Tips, "-"];
 sum += Tips;  
}
  • the question is that the code continues, "does not wait to execute the callback". I guess I still don’t quite understand how it works, so I’m not being able to implement

1 answer

0

Like you said and right "the question is that the code continues", so you need to redirect the application flow to continue after callback:

function valida2(done) {
  bootbox.prompt({
    title: "This is a prompt with a number input!",
    inputType: 'number',
    callback: done
  });
}


//a função que chama esse prompt
function ccck() {

  valida2(function(result) {
    // o fluxo da aplicação continua aqui:
    var Tips = parseInt(result);
    myArray[count] = [count + 1, 1, Tips, "-"];
    sum += Tips;
  });

}

So your code stays inside the callback, that is when the result is available.

Browser other questions tagged

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