How does this Promise program flow work in javascript?

In summary, the conversation discusses the syntax for Promises and how they are used in asynchronous JavaScript. The code shown creates a new Promise with a function that will either resolve or reject based on the comparison of two variables. If the comparison is true, the .then() method will be executed, and if it is false, the .catch() method will be executed. The conversation ends with the speaker expressing their difficulty in understanding asynchronous JavaScript and asking for a step-by-step explanation of the program flow. The responder confirms the speaker's understanding and provides no further information.
  • #1
shivajikobardan
674
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
  • #2
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

1. What is a Promise in JavaScript?

A Promise in JavaScript is an object that represents the eventual completion or failure of an asynchronous operation. It allows us to handle asynchronous code in a more organized and readable way by providing a structured approach to handling the result or error of an asynchronous task.

2. How does the Promise program flow work in JavaScript?

The Promise program flow in JavaScript begins with the creation of a Promise object which takes in a callback function with two parameters: resolve and reject. The code inside the callback function is executed and once completed, the promise is either resolved (successful) or rejected (unsuccessful) based on the outcome of the code. Then, the .then() or .catch() methods are used to handle the resolved or rejected promises respectively.

3. What is the purpose of using a Promise in JavaScript?

The purpose of using a Promise in JavaScript is to handle asynchronous code in a more organized and efficient manner. It allows us to avoid callback hell and provides a structured approach to handling the result or error of an asynchronous task. Promises also make it easier to handle errors and provide a better understanding of the code flow.

4. Can a Promise be in a pending state forever?

Yes, a Promise can be in a pending state forever if the code inside the promise never resolves or rejects. This can happen if there is an infinite loop or if the code is stuck in an asynchronous operation that never completes. However, it is important to properly handle promises to avoid this scenario and ensure that the code is properly executed.

5. How do you handle multiple Promises in JavaScript?

There are a few ways to handle multiple Promises in JavaScript. One way is to use the Promise.all() method which takes in an array of promises and returns a single promise that is resolved when all of the promises in the array are resolved. Another way is to use the Promise.race() method which takes in an array of promises and returns a single promise that is resolved or rejected as soon as one of the promises in the array is resolved or rejected.

Similar threads

  • Programming and Computer Science
Replies
9
Views
997
  • Programming and Computer Science
Replies
2
Views
995
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Sticky
  • Programming and Computer Science
Replies
13
Views
4K
Replies
1
Views
929
  • Programming and Computer Science
Replies
6
Views
3K
Replies
12
Views
2K
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top