jquery - Javascript - Uncaught (in promise) -
i have function on click use sweetalert2. function:
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 'overlay', 'cancel', 'close', 'esc', 'timer' if (dismiss === 'cancel') { // might handle 'close' or 'timer' if used // ignore } else { throw dismiss; } }) }
everything works fine error timer:
sweetalert2.min.js:1 uncaught (in promise) timer
how can avoid that, doing wrong?
the problem should never call function returns promise without doing promise. in case promise-returning functions swal
, $.post
. if ignore returned promise you're not waiting complete. then
handlers can return promise continue promise chain, this:
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 'overlay', 'cancel', 'close', 'esc', 'timer' if (dismiss === 'cancel') { // might handle 'close' or 'timer' if used // ignore } else { throw dismiss; } }) .catch(function(err) { console.error(err); throw err; }) }
Comments
Post a Comment