1

I'm trying to get the return value in sweetalert but I got this

Promise {<pending>}
  >__proto_:Promise
  [[PromiseStatus]]: "resolved"
  [[PromiseValue]]:true

from this code

var ret = swal({
  title: "Conflict",
  text: "You have the same schedule with "+response.data.title,
  icon: "warning",
  buttons: {
    cancel: {
      text: "Save Anyway",
      value: true,
      visible: true
    },
    confirm: {
      text: "Cancel",
      value: null
    }
  }
})
.then((value) => {
  if (value == true) {
    return true;
  } else {
    return false;
  }
});

console.log(ret); // Nag stop ko ani kay wala ko kabalo pa sa promise

What I want is to get to get the boolean value from the sweetalert. I'm trying this for hours but no luck.

I also tried this

console.log(ret.resolved);
console.log(ret.PromiseStatus);

and similar other but, got undefined Has anyone tried what I did?

Fil
  • 8,225
  • 14
  • 59
  • 85
  • 1
    Possible duplicate of [How to return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Serge K. Nov 14 '17 at 09:11

1 Answers1

1

The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.

A Promise is in one of these states:

pending: initial state, neither fulfilled nor rejected.

fulfilled: meaning that the operation completed successfully.

rejected: meaning that the operation failed.

Promise.resolve(value) Returns a Promise object that is resolved with the given value.

If the value is a thenable (i.e. has a then method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value.

Generally, if you don't know if a value is a promise or not, Promise.resolve(value) it instead and work with the return value as a promise.

An example for what you are trying to achieve, using the static Promise.resolve method

Promise.resolve('Success').then(function(value) {
  console.log(value); // "Success"
}, function(value) {
  // not called
});

Sources: Mozilla documentation

amyloula
  • 1,556
  • 2
  • 17
  • 26