【发布时间】:2018-01-09 18:41:48
【问题描述】:
正在寻找但找不到任何答案。
我遇到了 Promise() 问题。
一直在使用 Sweetalert,所以创建了一个脚本:
<button onclick="showAlert();">some button</button>
<script>
/**
* @constuctor
*/
function Obj() { };
/**
* @method getInst
* @param arguments
*/
Obj.prototype.getInst = function () {
swal.apply(this, arguments);
};
/**
* @instance
*/
var newInst = new Obj();
function showAlert() {
newInst.getInst({
title: "Good job!",
text: "You clicked the button!",
icon: "success"
});
};
并将参数应用于 swal 对象工作正常。然而,应用与承诺(Sweetalert 以这种方式工作)对抗的回调不能被链接并应用于对象并且不起作用:
function showAlert() {
newInst.getInst("Click on either the button or outside the modal.")
.then((value) => {
swal(`The returned value is: ${value}`);
});
};
感谢您的回复。
【问题讨论】:
-
为了让它工作,你的
getInst必须返回一个新的Promise。 -
您的
getInst方法缺少return语句。 -
是的,也注意到了这一点——尽管添加这个并没有解决问题。