Javascript - Uncaught (in Promise)
I have a function on click for which I use sweetalert2. This is the function: publish = function (article) { swal({ title: 'Skal du publisere?', text: null, ty
Solution 1:
The problem is that you should generally never call a function that returns a promise without doing something with that promise. In this case the promise-returning functions are swal
and $.post
. If you ignore the returned promise then you're not waiting for it to complete. Your then
handlers can return a promise to continue the promise chain, like this:
publish = function (article) {
returnswal({
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(){
returnswal({
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;
})
}
Solution 2:
You need to add a rejection handler to the Promise. Alternatively, you can use .catch(swal.noop)
as a quick way to simply suppress the errors:
swal('...')
.catch(swal.noop);
This issue is mentioned in the package documentation: https://github.com/limonte/sweetalert2#handling-dismissals
Also, there's the closed issue about the subject: limonte/sweetalert2#221
Post a Comment for "Javascript - Uncaught (in Promise)"