Issue with resolve and reject within a promise

I am not sure about how the ‘reject’ parameter is used. I have this code:

window.doritIsHere = true
var aMatz = new Promise(function Matz(resolve, reject) {
 setTimeout(function() {
   if (window.doritIsHere === true) {
   } else {
 }, Math.random() * 10000)

aMatz.then(function() {
 alert('Matz is here!')

alert("We're done here")

If I only have the ‘resolve’ parameter and I make everything true or, everything false, we get to “Matz is here!” And, if I only use ‘reject’ as a parameter and I use true and false or, false and true, we get to “Matz is here!” But if I use ‘resolve’ and ‘reject’ as parameters and then I use a true and then a false ie it shouldn’t resolve, nothing happens ie no “Matz is here!”. Basically, the reject should kick in but it doesn’t. In reading about it, the only thing that I noticed was that only resolve or reject can happen - not both. So, the question is: what is the point of using, if…else in this code because it doesn’t seem to achieve anything. Could someone help me to come to terms with the way to use ‘resolve’ and ‘reject’? Thanks - Al

to determine whether to call the resolve or reject callback.

Try console.log’ing aMatz after your code executes.

And it doesnt.

Error is a generic type, and your code isnt inside a try-catch, so your Javascript console should have thrown an Uncaught Error…error. Your reject does execute, if you set window.doritIsHere to false.

Hi @albfleck, it does but you’re not actually handling the promise rejection – if you open the console of your brower dev tools you’ll see an error message complaining that you forgot to catch() that. So you’d consume the promise like so:

  .then(function() {
    console.log('Matz is here!')
  .catch(function (reason) {
    console.error('An error occurred:', reason)
  .finally(function () {
    console.log("We're done here")

BTW you might also reject with an actual error message, like e.g.

reject('something went wrong')