0

Can you explain me something?

const promise1 = new Promise((resolve, reject) => {
  setTimeout(() => {
    if(true) {
      resolve('succes')
    } else {
      reject('failure')
    }
  }, 4000)
})

promise1.then(result => console.log(result))

Why after that i got 'succes' on console and something like this

Promise {<fulfilled>: undefined}

Why there is undefined if my promise returning 'succes' string value? This is beacause of setTimeout nesting? How could I fix this?

Mateusz W
  • 51
  • 1
  • 7
  • I've just tried your code and it works ok. Resolved into the `succes` and logged `succes`. _Btw it's succes**s**._ – Jax-p Nov 20 '20 at 14:19
  • 2
    Because your promise didn't return anything - e.g. `console.log` returns undefined – Adam Jenkins Nov 20 '20 at 14:20
  • We really need a canonical for "What does console.log return? Why is it when I use it I get undefined?" – VLAZ Nov 20 '20 at 14:22

3 Answers3

3

by doing following adjustments:

const promise1 = new Promise((resolve, reject) => {
  setTimeout(() => {
    const stat = true;
    if(stat) {
      resolve('success')
    } else {
      reject('failure')
    }
  }, 4000)
})

promise1.then(
    result => {console.log('resolve', result); return result;},
    result => {console.log('reject', result); return result;}
)

you will have both log and promise return value on dev tools;

WHY?

undefined is the result of the implicit return value the callback (return;), when a function doesn't return anything explicitly, js engine will add implicit return statement to the end of function body.

Mechanic
  • 5,015
  • 4
  • 15
  • 38
  • 1
    A tip about this answer is that `then` is responding to `resolve` stat and there's no handler for `reject` stat. the `then` part should edited to `.then(result => {console.log('resolved:', result); return result;}, result => {console.log('reject:', result); return result;} )` – Saghachi Mar 03 '23 at 19:01
1

@ Mateusz W

1- The first log 'success' comes from the 'console.log(result)' that you have written.

2 -The second log 'Promise {: undefined}' is refering to the 'promise1.then(result => console.log(result))', because the 'then' function return itselelf a promise that resolves the result that you return inside it (in your case you return nothing inside then).

To be sure verify the following code :

promise1.then(result => console.log(result)).then(result1 => {})

you will obtain the following result

succes
undefined
Promise {<fulfilled>: undefined}

The undefined after the success is the one that you find in your 'Promise {: undefined}'

that correspond here to result1.

Do not confuse with this Promise {<fulfilled>: undefined} that you see here (result of my code) because this is referring to the second then that I chained to your then

In summary: you may were confused and thought that the Promise {<fulfilled>: undefined} that you saw in console is referring to you promise1 const. No it is not, it is the return of the then function.

0

To be fair, you aren't showing the code where you get

Promise {<fulfilled>: undefined}

But I imagine it could look something like this:

promise1.then(result => {
   console.log(result)
   console.log(promise1); // this line here
})

If you do this instead:

promise1.then(result => {
   console.log(result)
   console.log(promise1); 
   return result; // now this line here
})

Then you will see:

Promise {<fulfilled>: "succes"}
Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
  • 1
    OP probably just typed their exact code into the console, and the console gave them the return value of the last line… – deceze Nov 20 '20 at 14:31
  • @deceze - but the promise isn't fulfilled at that point - so they can't see a `Promise {: undefined}` using that, they'd see a `Promise{}` – Adam Jenkins Nov 20 '20 at 14:31
  • It might be a live-updating object/they only see that when expanding it after it *is* fulfilled? – deceze Nov 20 '20 at 14:33
  • @Adam "*but the promise isn't fulfilled at that point*" [Is Chrome's JavaScript console lazy about evaluating arrays?](https://stackoverflow.com/q/4057440) https://i.imgur.com/zTwcGTG.png – VLAZ Nov 20 '20 at 14:37
  • @deceze - was certainly my first thought as well. But I checked before I put that in my answer, this is not the current behavior in Firefox, Chrome, or Safari. Although I know realize maybe the OP meant something other than console. – Adam Jenkins Nov 20 '20 at 14:37