【发布时间】:2020-03-21 20:02:43
【问题描述】:
我是一名新的 ecmaScript6 学生。
我需要在封装库函数时链接到“then”promise。
Swal 是 sweetAlert2 函数,用于提问并获得用户的响应,是/否。
这就是我想要做的;
class MyLib {
constructor() {
}
static askQuestion(title, message){
Swal.fire({
title: title,
text: message,
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
return result;
})
}
}
然后像这样调用这个函数;
MyLib.askQuestion("Are you sure?", "Are you sure you want to delete this ?").then(alert(result));
但是当然;由于警报(result),在运行时控制台上给我“.askQuestion(...) is undefined”。
如何在 es6 中链接两个然后函数?
【问题讨论】:
-
您忘记了
askQuestion中的return语句。 (并且then()回调是毫无意义的,因为它只是返回未修改的承诺的结果) -
从 askQuestion 中删除
then()并直接返回Swal.fire。 -
非常感谢@SajeebAhamed。它奏效了。
-
不客气。
标签: javascript ecmascript-6 es6-promise sweetalert2