github sweetalert2/sweetalert2 v4.0.0
Using Promise, the right way

latest releases: v11.11.0, v11.10.8, v11.10.7...
7 years ago

Breaking change in 4.0.0

SweetAlert2 now rejects Promise reject(dismiss) in cases:

  • Cancel button was clicked (dismiss will be "cancel")
  • Overlay click ("overlay")
  • Esc press ("esc")
  • Timer ("timer")

Before 4.0.0 Promise handling:

swal({title: 'Are you sure?', showCancelButton: true}).then(
  function(isConfirm) {
    if (isConfirm === true) {
      // handle Confirm button click
    } else if (isConfirm === false) {
      // handle Cancel button click
    } else {
      // Esc, close button or outside click, isConfirm is undefined
    }
  }
);

Starting with v4.0.0 Promise will be resolved in case of Confirm button was clicked and rejected with dismiss parameter on all other cases:

swal({title: 'Are you sure?', showCancelButton: true}).then(
  function(result) {
    // handle Confirm button click
    // result is an optional parameter, needed for modals with input
  }, function(dismiss) {
    // dismiss can be 'cancel', 'overlay', 'esc' or 'timer'
  }
);

Don't miss a new sweetalert2 release

NewReleases is sending notifications on new releases.