confirm
halts the codes execution of the browser, until a button gets clicked. That's why confirm
can return synchronously and behave like a regular function. You cannot halt the browsers execution from within JS, so this is entierely impossible (or if you do, the user cannot input anything).
You could however create and return a promise (which can then be resolved asynchronously):
function func(){
return new Promise((resolve, reject) => {
$('body').once('click', '.true', function() { // Only listen to *one* click instead of keeping this listener forever
resolve(true); // resolve the promise if clicked
});
$('body').once('click', '.false', function() {
resolve(false);
});
});
}
That can then be used as:
async function test(){ // has to be async, so that the execution can be haltet
var r = await func(); // halts till click
if(!r) { // reads "if not r"
alert('false');
} else alert('it works');
}
test();
Read On