How does this Promise program flow work in javascript?

  • #1
Promises syntax that I learnt:

JavaScript:
let p = new Promise(function (resolve, reject) {
  let x = 20, y = 20;
  if (x == y) {
    resolve();


  }
  else {
    reject();
  }
})


p.then(function () {
  console.log("that's correct");
})


  .catch(function () {
    console.log("that's not correct");
  })


I don't even understand what happens here, the flow control of here. My mild guess is when resolve is called .then part happens, and when reject is called .catch part happens.


I've like read countless articles, banged my head against the wall but it's still not getting inside my head. I feel so dumb for asynchronous javascript.

Please help me understand the program flow step by step in more detail.
 
  • #2
I don't even understand what happens here, the flow control of here. My mild guess is when resolve is called .then part happens, and when reject is called .catch part happens.
That's absolutely right, there's nothing more to say here!
 
  • Informative
  • Like
Likes shivajikobardan and berkeman

Suggested for: How does this Promise program flow work in javascript?

Replies
2
Views
806
Replies
9
Views
804
Replies
2
Views
772
Replies
1
Views
776
Replies
11
Views
901
Replies
11
Views
628
Replies
4
Views
756
Replies
2
Views
900
Replies
5
Views
955
Replies
9
Views
996
Back
Top