Javascript - 未捕获(承诺)
Javascript - Uncaught (in promise)
我有一个点击功能,我使用 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) timer
我怎样才能避免这种情况,我做错了什么?
问题是您通常不应该在不对该承诺做任何事情的情况下调用一个 return 承诺的函数。在这种情况下,promise-returning 函数是 swal
和 $.post
。如果您忽略 returned 承诺,那么您就不会等待它完成。您的 then
处理程序可以 return 继续承诺链的承诺,如下所示:
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;
})
}
您需要向 Promise 添加拒绝处理程序。或者,您可以使用 .catch(swal.noop)
作为简单抑制错误的快速方法:
swal('...')
.catch(swal.noop);
软件包文档中提到了这个问题:https://github.com/limonte/sweetalert2#handling-dismissals
此外,还有关于该主题的已关闭问题:limonte/sweetalert2#221
我有一个点击功能,我使用 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) timer
我怎样才能避免这种情况,我做错了什么?
问题是您通常不应该在不对该承诺做任何事情的情况下调用一个 return 承诺的函数。在这种情况下,promise-returning 函数是 swal
和 $.post
。如果您忽略 returned 承诺,那么您就不会等待它完成。您的 then
处理程序可以 return 继续承诺链的承诺,如下所示:
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;
})
}
您需要向 Promise 添加拒绝处理程序。或者,您可以使用 .catch(swal.noop)
作为简单抑制错误的快速方法:
swal('...')
.catch(swal.noop);
软件包文档中提到了这个问题:https://github.com/limonte/sweetalert2#handling-dismissals
此外,还有关于该主题的已关闭问题:limonte/sweetalert2#221