Java How does this Promise program flow work in javascript?

Click For Summary
The discussion centers on understanding the flow control of JavaScript Promises. A Promise is created with a function that takes two parameters: resolve and reject. In the provided code, if the condition (x == y) is true, the resolve function is called, triggering the .then method, which logs "that's correct". If the condition is false, reject is called, activating the .catch method, which logs "that's not correct". The main point of confusion is clarified: when resolve is invoked, the .then part executes, and when reject is called, the .catch part executes. This confirms the user's understanding of how Promises work in asynchronous JavaScript.
shivajikobardan
Messages
637
Reaction score
54
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.
 
Technology news on Phys.org
shivajikobardan said:
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
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 18 ·
Replies
18
Views
6K