Two buttons in a modal, each triggers a simple function. How can I use a promise to trigger another function after one of the two buttons are pressed?

Viewed 25

I have a button called "Test" that brings up a modal.

$("#testbutton").on("click",function(){...})

This function (...) reveals a modal using css visibility. Each button has a simple click function

$("#choicemodal").css("visibility","visible");
$("#choicemodal-choice1").one("click",function() {console.log("you clicked the first choice");
$("#choicemodal-choice2").one("click",function() {console.log("you clicked the second choice");

I would like to be able to use a promise to create an asynchronous function that waits for whichever function the user chose to finish, and then execute something else. I'd love to be able to use information from the choice as well, something like

function() {console.log("Thanks for choosing choice " + choice)

As I've been practicing promises, I've been able to get it working when the function executed is INSIDE the promise, something simple like

const ourPromise = new Promise((resolve, reject) =>
{

function dieToss(){

return Math.floor(Math.random()*6 +1)
}



var n = dieToss()


if(n===6){
resolve("Nice, threw a 6!")
}

else if(n===1){
resolve("darn, it's a 1")
}

else {
reject("I threw a " + n +" and I'm somewhere in the middle.")
}



}).then(response => console.log(response)).catch(error => console.log(error))

but I'm not sure how to do that when click events are involved, less so when "one of two options" is being waited to complete. Any tips would be appreciated. Thanks!

0 Answers
Related