如何在不使用承诺的情况下重写 swal 提示

How to rewrite swal prompt without using promises

我想在删除项目之前为我的警报提供一个提示选项。但是它在 IE 中不起作用。我怎样才能避免使用这个承诺(.then())?

swal 的例子是这样的:

swal({
  title: "Are you sure?",
  text: "Once deleted, you will not be able to recover this imaginary file!",
  icon: "warning",
  buttons: true,
  dangerMode: true,
})
.then((willDelete) => {
  if (willDelete) {
    swal("Poof! Your imaginary file has been deleted!", {
      icon: "success",
    });
  } else {
    swal("Your imaginary file is safe!");
  }
});

假设 swal 的其余部分可以在 IE 上运行(您检查过了吗?),那么您需要:

  1. 添加一个Promise polyfill,其中有很多很容易得到,并且

  2. 不使用箭头函数,因为 IE 不支持箭头函数

例如,使用 Promise polyfill,这是 IE 的有效代码:

swal({
  title: "Are you sure?",
  text: "Once deleted, you will not be able to recover this imaginary file!",
  icon: "warning",
  buttons: true,
  dangerMode: true,
})
.then(function(willDelete) { // <=== Only change is here
  if (willDelete) {
    swal("Poof! Your imaginary file has been deleted!", {
      icon: "success",
    });
  } else {
    swal("Your imaginary file is safe!");
  }
});

或者您可以使用 Babel 之类的工具将 ES2015+ 代码转换为 ES5 兼容,并且可以选择包含 polyfill(用于 Promise 之类的东西)。 Babel 会为您处理转换箭头函数。