【问题标题】:javascript async sweetAlertjavascript 异步 sweetAlert
【发布时间】:2018-11-30 12:19:29
【问题描述】:

所以我必须尝试创建一个在返回之前等待用户输入的异步函数,但是,我不太确定该怎么做:

  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 创建以下弹出窗口:

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

谁能指出我正确的方向?

【问题讨论】:

    标签: javascript jquery async-await


    【解决方案1】:

    这样试试:

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

    【讨论】:

    • 输入呢? let result = await swal('ACAD Helper',"Amount to Submit:", { content: "input", });
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 2017-08-01
    相关资源
    最近更新 更多