t4t5 / sweetalert

A beautiful replacement for JavaScript's "alert"
https://sweetalert.js.org
MIT License
22.4k stars 2.84k forks source link

Stop the execution until response is obtained #924

Open prwinkmr opened 4 years ago

prwinkmr commented 4 years ago

if(name==="PK"){ x=true; } else{ //after this alert is on screen I want execution of to be freezed until I get a response from user // if she/he will click yes I can assign true to x in callback or else false and then I want to proceed // this freezing is required because if by default name is PK then no need to display alert // If I am unable to freeze till response I am forced to repeat the code. //Please suggest some solution to this swal("Query","Are you PK",...) }

if(x){ console.log("Welcome PK!!!"); }

WilliamOConnell commented 4 years ago

Not sure I 100% understand what you're asking, but I think await is what you want? For example:

async function ask() {
    let x = await swal({
        title: "Are you PK?",
        buttons: ["no","yes"]
    });
    if (x) {
        console.log("Welcome PK!!!");
    }
}
ask();