Understanding Promises in JavaScript: Part 1

Search for a command to run...

Thanks, yeah right I will 😊
I'm sharing what I've learnt from a course I took: "JavaScript: The Hard Parts v2" from [Frontend Masters](https://frontendmasters.com/courses/javascript-hard-parts-v2/) by Will Sentance.
Before proceeding into what I've for this article, let's recap on what was discussed in my previous article: Promises in JavaScript: Part 1. Whenever an asynchronous instruction is given to be executed, it's going to the Web Browser features (API) in...
With the rise of AI agents, vibe coding, and the advancement of software development tools, it's never been easier to build applications quickly. However, these advancements come at a cost to software

Personal highlights along with Work highlights in 2024

💡 Originally posted here. In the intricate world of web design today, typography serves as a powerful means of expression, improving site usability and it goes beyond the realms of font choice and sizes. In this article we’ll explore a sub-section...

Ever spent hours wrestling with images that refuse to stay put in your web layouts? You're not alone. In this comprehensive guide, we will unravel the mystery behind different image centering methods, from the basic and widely used to the more advanc...

Tailwind CSS recently launched some awesome updates in v3.4, supporting CSS features like Dynamic viewport units, subgrid supports and other features, which we'll go over in this article. It's so easy to be carried away by using the already-known fea...

Hello once again, this article is all about what I understood from a course I took Javascript: The Hard Parts from FrontendMaster which I've been writing about in this series.
From the series, so far, I've written about Thread of Execution, Memory, Execution Context, Call Stack, Callback queue, Event loop, WEB API and then Asynchronous Javascript.
I'll advise you to check out the previous blog posts before proceeding anyway let's proceed.
I'll explain what a Promise is, what are the properties a Promise object has and how it's asynchronous.
Now, let's get to the main topic;
A Promise is a special object that may produce a single value at some time in the future. Since the single value is expected, later on, that makes the concept about promises asynchronous.
Recap from my previous post on Asynchronous Javascript, whenever an asynchronous instruction is to be executed, it goes to the Web Browser and there isn't any way to tell Javascript about the progress of the asynchronous operation before it moves to the Call stack for execution.
But, with Promises, you'll be able to tell Javascript the progress of what's going on in the Web Browser's background.
Here's a quote by Will Sentance the instructor of the course:
"when you trigger something in the background, don't just throw it out there. But have it have some sort of consequence in JavaScript memory as well"
Since it'll be better to have some consequence based on what's happening on the Browser to JavaScript, Two-pronged facade function was initialized.
What's meant by Two-pronged facade function you might ask:
What request/task you might ask?
In this case, I'm talking about speaking to the internet using Fetch / XHR, labelled as Network Request by the Web Browser.
Fetch speaks to the internet while in the Web Browser background and then returns a promise object to Javascript. The promise object shows the progress of the data to be fetched.
Let's look at an example:
function display(data) {
console.log(data);
}
const futureData = fetch('https://twitter.com/jomz/tweets/1')
futureData.then(display);
console.log("Hello");
If you'll prefer viewing an illustration demonstrating the instructions above, click here
From the instructions above:
display() is declared and stored in the Global Memory.Then a variable futureData is assigned a value fetch to go to some URL (Uniform Resource Locator) and get a data (response data).
The fetch returns an object which is a promise with three properties/keys (for this article, we'll be looking at two properties) which are:
value which has no value for now
onFulfilled which is an empty array for now also.futureData holds an object (promise object) which is JavaScript consequence of getting something through the internet.
The consequence happening on the Web Browser will be a Network Request which holds the domain name (twitter.com) of the URL and also the path (jomz/tweets/1) which the domain name is linked to.
Note: by default
fetchusesGETsaying get some data from the internet.
When whatever data fetched has been retrieved (known as response data), its get added to the promise object value property.
Note: the time taken to speak to the internet isn't known, it could be days on a slow network :) but whenever the response data is gotten it'll be inputted (passed as parameter) by default into a function in the
onFulfilledproperty.
futureData.then(display): this line of code is saying, whatever response data that'll be received, should be added to the function display in the onFulfilled property array of futureData, also having access to the value property value.
console.log("Hello") is executed next.
Now, after 200ms, finally the Web Browser has received a response data ("hi, everyone") which is then passed to the futureData value property and then the function (display()) in onFulfilled property is invoked and added to the Call Stack also having the response data as parameter and then logs hi, everyone.
Remember when ever a Function is invoked a brand new Function Execution Context is created, more on that here
OMG 🤯, that's a lot of details. Here is a graphical illustration below of all the above explanations.

They're more advanced ways of using promises from the little research I made but this is the fundamentals of what promises is all about.
Here are further concepts to note:
When the value property in the promise object is empty, this state is known as Pending.
When the value property in the promise object has a value, this state is known as Resolved.
Lastly, the Rejected state is when something has gone wrong with the data that's meant to be recieved. I'll write about this state in the next article.
Also, in the next article, I'll be writing about Microtask Queue and also looking at more complicated example.
Thanks for reading 🥳, if you've read to this point, see you at the next article.
P.S. To be frank, I haven't got my hands dirty with these concept, but I feel learning and sharing what I've learnt will be a great use to me when I finally start appling these concepts or what do you think? Leave in the comment below.