How does this Promise program flow work in javascript?

Click For Summary
SUMMARY

The discussion centers on understanding the flow of JavaScript Promises, specifically the syntax and behavior of the Promise constructor. A Promise is created using the `new Promise` syntax, which takes a function with `resolve` and `reject` parameters. When the `resolve` function is called, the `.then` method executes, while the `.catch` method executes when `reject` is called. This confirms the asynchronous nature of JavaScript and the handling of success and failure states in Promise-based programming.

PREREQUISITES
  • Understanding of JavaScript syntax and functions
  • Familiarity with asynchronous programming concepts
  • Knowledge of Promise objects in JavaScript
  • Basic debugging skills in JavaScript
NEXT STEPS
  • Study the JavaScript Promise API documentation
  • Learn about async/await syntax in JavaScript
  • Explore error handling in Promises
  • Investigate Promise chaining and its applications
USEFUL FOR

JavaScript developers, particularly those working with asynchronous code, and anyone seeking to deepen their understanding of Promise behavior and flow control.

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   Reactions: shivajikobardan and berkeman

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