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
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top