我有点击功能,我使用sweetalert2。这是功能:
publish = function (article) {
swal({
title: "Skal du publisere?",
text: null,
type: "info",
showCancelButton: true,
cancelButtonText: "Avbyrt",
cancelButtonColor: '#FFF',
confirmButtonColor: "#2E112D",
confirmButtonText: "Ja, publisere"
}).then(function(){
var articleId = $(article).val();
$.post("/admin/articles/publish/article", {
'_token' : $('meta[name="csrf-token"]').attr('content'),
'articleId': articleId
}).done(function(){
$(article).hide();
return swal({
type: 'success',
title: 'Du har publisert den artikkel.',
showConfirmButton: false,
timer: 1000
});
}).fail(function() {
return swal({
type: 'warning',
title: 'Noeting gikk feil, prov igjen',
showConfirmButton: false,
timer: 1000
});
});
}, function(dismiss) {
// dismiss can be 'overlay', 'cancel', 'close', 'esc', 'timer'
if (dismiss === 'cancel') { // you might also handle 'close' or 'timer' if you used those
// ignore
} else {
throw dismiss;
}
})
}
一切正常但我得到了计时器的错误:
sweetalert2.min.js:1 Uncaught(in promise)计时器
我怎么能避免这种情况,我做错了什么?
答案 0 :(得分:1)
问题是你通常不应该调用一个函数来返回一个promise,而不用做那个promise。在这种情况下,承诺返回函数是swal
和$.post
。如果您忽略了返回的承诺,那么您就不会等待它完成。您的then
处理程序可以返回继承承诺链的承诺,如下所示:
publish = function (article) {
return swal({
title: "Skal du publisere?",
text: null,
type: "info",
showCancelButton: true,
cancelButtonText: "Avbyrt",
cancelButtonColor: '#FFF',
confirmButtonColor: "#2E112D",
confirmButtonText: "Ja, publisere"
}).then(function(){
$(article).hide();
var articleId = $(article).val();
return $.post("/admin/articles/publish/article", {
'_token' : $('meta[name="csrf-token"]').attr('content'),
'articleId': articleId
}).then(function(){
return swal({
type: 'success',
title: 'Du har publisert den artikkel.',
showConfirmButton: false,
timer: 1000
}).catch(function(timeout) { });
});
}, function(dismiss) {
// dismiss can be 'overlay', 'cancel', 'close', 'esc', 'timer'
if (dismiss === 'cancel') { // you might also handle 'close' or 'timer' if you used those
// ignore
} else {
throw dismiss;
}
})
.catch(function(err) {
console.error(err);
throw err;
})
}
答案 1 :(得分:0)
您需要向Promise添加拒绝处理程序。或者,您可以使用.catch(swal.noop)
作为简单方法来简单地抑制错误:
swal('...')
.catch(swal.noop);
包文档中提到了此问题:https://github.com/limonte/sweetalert2#handling-dismissals
此外,还有关于该主题的已结束问题:limonte/sweetalert2#221