Search code examples
javascriptsweetalert2

Sweetalert Promise issue


Was looking but could not find any answer.

Am having a Promise() issue.

Have been using Sweetalert, so have created a script:

<button onclick="showAlert();">some button</button>
<script> 
/**
 * @constuctor
*/
function Obj() { };

/**
 * @method getInst
 * @param arguments
 */
Obj.prototype.getInst = function () {
  swal.apply(this, arguments);
};

/**
 * @instance
 */
var newInst = new Obj();

function showAlert() {
  newInst.getInst({
    title: "Good job!",
    text: "You clicked the button!",
    icon: "success"
  });
};

and appling the arguments to the swal object is working properly. However appyling the callback working against promises (Sweetalert works in such way) cannot be chained and applied to the object and does not work:

function showAlert() {
  newInst.getInst("Click on either the button or outside the modal.")
  .then((value) => {
  swal(`The returned value is: ${value}`);
});
};

Thanks for reponse.


Solution

  • OK, have done this.

    if anyone is interested all you need to do is to pass the Sweetalert object diectly to the constuctor:

    Obj.prototype.getInst = function () {
      swal(opts);
    };