Java How does this Promise program flow work in javascript?

AI Thread 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
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top