Run Javascript Promises - It's working, but is the code "best practice"?

Hey all,

I’ve been trying to implement SweetAlert2, and after trying for hours to figure out how to stop an inline flow from proceeding until my JavaScript function had finished, and reading somewhere here that all steps are essentially promises, I’ve finally sorted my code out.

However, not being all that familiar with Promises (more async/await here), I’d like to know if the below is actually decent code, or whether it’s a hodgepodge that somehow works.

function swal_confirm(alerticon, alerttitle, showdeny, denytext, showcancel, canceltext, confirmtext, confirmedcallback, denycallback) {
  return new Promise(function(resolve, reject) {
      Swal.fire({
      title: alerttitle,
      showDenyButton: showdeny,
      showCancelButton: showcancel,
      confirmButtonText: confirmtext,
      cancelButtonText: canceltext,
      denyButtonText: denytext,
      icon: alerticon
    }).then((result) => {
      if (result.isConfirmed && confirmedcallback) {
        Swal.fire('Saved!', '', 'success');
        resolve(true);
      } else if (result.isDenied && denycallback) {
        Swal.fire('Changes are not saved', '', 'info');
        resolve(false);
      }
    })
  })
}

This works. It returns true or false, depending on the confirmation action, and my flow proceeds according to plan.

image

Thanks for any input y’all might have.

Cheers
Michael

Community Page
Last updated: