473,395 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do promises program flow work?

12 Nibble
Promises syntax that I learnt:


Expand|Select|Wrap|Line Numbers
  1. let p = new Promise(function (resolve, reject) {
  2.   let x = 20, y = 20;
  3.   if (x == y) {
  4.     resolve();
  5.  
  6.   }
  7.   else {
  8.     reject();
  9.   }
  10. })
  11.  
  12. p.then(function () {
  13.   console.log("that's correct");
  14. })
  15.  
  16.   .catch(function () {
  17.     console.log("that's not correct");
  18.   })
  19.  
  20.  
  21.  
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.
Dec 19 '22 #1
1 4812
Dormilich
8,658 Expert Mod 8TB
then() is called, when the promise gets fulfilled (resolved or rejected), i.e. the callback does not throw an error. The difference between resolve() and reject() is that resolve() will trigger the first callback in then() and reject() will trigger the second callback (if one was defined).

Any errors you throw inside a callback will trigger the callback in catch().

https://developer.mozilla.org/en-US/...s/Promise/then
https://developer.mozilla.org/en-US/.../Promise/catch
Dec 19 '22 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Albert Ahtenberg | last post by:
Hello, I had some bad experience with code organization and script functionality in writing my php based applications. And as the applications get bigger in scale it gets even worse. Therefore,...
4
by: Bruce W...1 | last post by:
A scripting newbie question... I'm trying to understand some code I found. This script conducts a poll and writes the results to a text file. The following statement is part of the source file. ...
4
by: Roland | last post by:
I was wondering, what happens when you have an onclick event and an error occurs in it: In an <a> element: onclick="zoomFullExtent(); return false;" I know that there is an error happening...
11
by: | last post by:
I repeatedly get into the situation where i need to take action accordingly to input in form of a char*, and have found two manners of approaching this, i'd appretiate pointers as to which is the...
7
by: jacob navia | last post by:
Suppose that you want to know where your program has passed, i.e. the exact flow of the program. A brute force solution is to make an array of n lines, n being the number of lines in the...
1
by: Brett | last post by:
I'd like to have all of my documentation in one place. I use the following for documenting code: - attributes for certain types of documentation - use of the C# generated inline XML...
18
by: utab | last post by:
Dear all, I am making a system call to the well known Gnuplot with system("gnuplot"); gnuplot opens if I only supply this command but I would like to pipe that command line in my C++...
4
by: Mike | last post by:
Hi, I am building a windows app. I have two threads that continually run in the background pulling information from a website. The data I extract from the two separate threads needs to be put...
18
by: Andrew Gentile | last post by:
Hello, I would like to find a way of using scanf() and the Enter key to have user-controlled program flow. Currently, I have a couple of lines in my program which serves as a pause in the...
3
by: gloomer | last post by:
I'm having a bit of trouble with program flow. I'd love to explain more, but it seems the comments in my code do a better job. #!/usr/bin/perl use strict; use warnings;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.