Marc Rasmussen
Marc Rasmussen

Reputation: 20565

javascript async sweetAlert

So i have to try to create an async function that waits for a user input before returning, however, I am not quite sure how to do it:

  async createAlert() {
      return await swal({
          title: 'Are you sure?',
          text: "You won't be able to revert this!",
          type: 'warning',
          showCancelButton: true,
          confirmButtonText: 'Yes, delete it!',
          cancelButtonText: 'No, cancel!',
          reverseButtons: true
      }).then(function (result) {
          //user has answered we want to return the result
      })
  }

this jquery creates the following popup:

enter image description here

When the user presses either button the (then) part of the code is executed and here i want to return that result

Can anyone point me in the right direction?

Upvotes: 1

Views: 10865

Answers (1)

Fifciuu
Fifciuu

Reputation: 864

Try it like that:

async createAlert() {
    try{
      let result = await swal({
          title: 'Are you sure?',
          text: "You won't be able to revert this!",
          type: 'warning',
          showCancelButton: true,
          confirmButtonText: 'Yes, delete it!',
          cancelButtonText: 'No, cancel!',
          reverseButtons: true
      });
      // SUCCESS
      return result;
    }catch(e){
        // Fail!
        console.error(e);
    }
}

Upvotes: 5

Related Questions