javascript async sweetAlert

时间:2018-06-21 08:31:45

标签: javascript jquery async-await

因此,我必须尝试创建一个异步函数,该函数在返回之前等待用户输入,但是,我不确定如何做到这一点:

  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
      })
  }

此jquery创建以下弹出窗口:

enter image description here

当用户按下任一按钮时,将执行代码的{then)部分,在这里我想返回该结果

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:2)

尝试一下:

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);
    }
}