Search code examples
javascriptes6-promise

Promise always pending in Chrome console


Why the following promise never resolves?

const x= new Promise(resolve=>setTimeout(()=>resolve(12),5000));

By writing, in the console,

const x= new Promise(resolve=>resolve(12))

am getting

`Promise {<fulfilled>: 12}`

By writing

const x= new Promise(resolve=>setTimeout(()=>resolve(12),5000)) 

am getting

Promise {<pending>}

Thought, that should change in fulfilled after 5 secs. But, it does not. Why?


Solution

  • The console is just showing you a snapshot of the current state of the Promise. It is not watching for changes to its state.

    Why the following promise never resolves?

    You can clearly see this does resolve after 5 seconds:

    const x= new Promise(resolve=>setTimeout(()=>resolve(12),5000)) ;
    x.then(result => console.log(result));