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
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · 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