最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - javascript async sweetAlert - Stack Overflow

programmeradmin6浏览0评论

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:

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?

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:

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?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jun 21, 2018 at 8:31 Marc RasmussenMarc Rasmussen 20.6k83 gold badges223 silver badges383 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 5

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);
    }
}
发布评论

评论列表(0)

  1. 暂无评论