Uncaught (in promise) cancel using SweetAlert2

32,061

Solution 1

Update (Jan 2017): This issue has been fixed in v7: v7 upgrade guide ↗


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);

PS. the package you're using is called SweetAlert2, not SweetAlert. In future questions please mention it so you can get more relevant answers.

Solution 2

SweetAlert2 rejects the result promise when the cancel button is pressed. You can handle that:

swal({
  …
}).then(function(json_data) {
  …
}, function(dismiss) {
  if (dismiss === 'cancel') { // you might also handle 'close' or 'timer' if you used those
    // ignore
  } else {
    throw dismiss;
  }
})

If you don't need to do anything with the json_data, you might also use the catch method.

Solution 3

new Promise(function(resolve, reject) { is not necessary. $.post() returns a jQuery promise object.

Possible solution substitutes Promise.reject() for new Promise() constructor; removed .then() that was placed as an option to first swal() call; pattern appears to expect a Promise to be returned from preConfirm, though not certain what value is expected to be returned from .done() other than json_data.

swal({
  title: "Confirm",
  input: 'checkbox',
  inputValue: 0,
  type: "warning",
  inputPlaceholder: 'I agree to <a href="#blahblahMore"></a>',
  showCancelButton: true,
  confirmButtonText: 'Confirm',
  showLoaderOnConfirm: true,
  preConfirm: function(result) {
      if (result) {
        return $.post("my.php", {
          Data: data
        })
        .done(
          function(json_data) {
            var data_array = $.parseJSON(json_data);
            var moreDetails = '';
            var resulting = 'error';
            var details = "Transaction Declined"
            if (data_array["trxApproved"] == true) {
              resulting = 'success';
              details = "Confirmed"
              moreDetails = "<br>Approved<b>" + data_array["approved"] + "</b>" +
                "<br>Details Code: <b>" + data_array["detailsCode"] + "</b>";
            }
            swal({
              type: resulting,
              title: details,
              html: "<h1>Details: </h1>" + data_array["messagetext"] + moreDetails
            });
          }
        );
      } else {
          return Promise.reject('You must agree to our Terms & Conditions ');
      }
  },
  allowOutsideClick: false
});

Solution 4

you will need to catch the action for cancel

swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!'
}).then(function(json_data) {
  //delete item
}, function(dismiss) {
  if (dismiss === 'cancel' || dismiss === 'close') {
    // ignore
  } 
})

Solution 5

Adding catch(swal.noop); at the end swal function will solve this problem

For example:

swal({

}).then(function() {

}).catch(swal.noop);
Share:
32,061
Frankenmint
Author by

Frankenmint

Cause I lay it down THICK!!! Expert of nothing= Generalist of EVERYTHING!!!!!

Updated on February 01, 2020

Comments

  • Frankenmint
    Frankenmint over 4 years

    how do I properly escape the cancel button without throwing an error when using promises? My code throws an alert confirmation with a required checkbox. the code executes as it should to the user, but it throws an error in the console window:

    Uncaught (in promise) cancel

    //validation logic all passes...Now proceed to...
    
     else
        {
    
    //determine and parse Discounts
    
     var myLookup = document.getElementsByName("myLookup")[0].value;
    $.post( "findthem.php", {myLookup: myLookup })
      .done(function(json_data){
         var theResponse1 = $.parseJSON(json_data);
         myDiscountRate = theResponse1['ourDiscountFound'];
    
        }).then( function(callback){
    
        priceRate = priceRate * (1 - (.01 * myDiscountRate));
        newRate = priceRate.toFixed(2);
    }
    
    swal({
      title: "Confirm",
      input: 'checkbox',
      inputValue: 0,
      type: "warning",
      inputPlaceholder: 'I agree to <a href="#blahblahMore"></a> Your new Rate is :'+newRate,
      showCancelButton: true,
      confirmButtonText: 'Confirm',
      showLoaderOnConfirm: true,
      preConfirm: function(result) {
        return new Promise(function(resolve, reject) {
          if (result) {
            $.post("my.php", {
              Data: data
            })
            .done(
              function(json_data) {
                var data_array = $.parseJSON(json_data);
                var moreDetails = '';
                var resulting = 'error';
                var details = "Transaction Declined"
                if (data_array["trxApproved"] == true) {
                  resulting = 'success';
                  details = "Confirmed"
                  moreDetails = "<br>Approved<b>" + data_array["approved"] + "</b>" +
                    "<br>Details Code: <b>" + data_array["detailsCode"] + "</b>";
                }
                swal({
                  type: resulting,
                  title: details,
                  html: "<h1>Details: </h1>" + data_array["messagetext"] + moreDetails
                });
              }
            );
            resolve();
          } else {
              reject('You must agree to our Terms & Conditions ');
          }
        });
      },
      allowOutsideClick: false
      }).then(function(json_data) {
    
      })
    });