Let’s Talk About Promises in JavaScript!!!

Vikram Nayyar CS
2 min readOct 4, 2021

In today’s post, I’m going back to Vanilla JavaScript and we’re talking about the Promise Object!! These are used when handling asynchronous actions so without a doubt, you will run into them at some point! A common use case is when dealing with API’s and you have to wait for the request to be processed.

Check out my affiliate links to get yourself a discount:

CleverSetups:
https://cleversetups.com/?ref=y_OiTWvsl_9GD

Discount CODE: ‘VNCS’

PAPA:
Platinum: https://www.papareact.com/a/2147491765/fGcDDEcG
Diamond: https://www.papareact.com/a/2147491766/fGcDDEcG
Diamond 3 Month Split: https://www.papareact.com/a/2147491767/fGcDDEcG
GitHub Repo: https://www.papareact.com/a/2147491768/fGcDDEcG
Platinum 3 Month Split: https://www.papareact.com/a/2147491769/fGcDDEcG

As always, before I delve deeper into this topic, make sure to hit the applaud button and share it with your friends as that will help this blog post to reach more people!!

What is a Promise?

A promise is a JavaScript object containing ‘Producing Code’ and ‘Consuming Code’.

Producing Code — Code that takes time.

Consuming Code — Code that must wait for a result.

JS Promises operate similarly to real life ones. Initially they’re pending and then they are either fulfilled (success) or rejected (not been kept, there’s an error!).

Promises have 2 properties: ‘State’ and ‘Result’.

The result is either undefined, a result value or an error object.

See the diagram for more details!!

Methods:

.then() follows a successful promise, usually an additional step!

For e.g., check out the pseudocode below:

fetch(“API_ENDPOINT”).then(response => response.json).then(data => console.log(data))

.catch() catches errors.

Examples:

Waiting for a timeout.

Waiting for a file.

Final Things:

As always, remember to applaud and share this post, helping it reach more people!!!

Follow Vikram Nayyar CS on all platforms!!!

Hashtags:

#computerscience #coding #programming #technology #python #programmer #computer #developer #tech #coder #java #javascript #code #codinglife #webdeveloper #softwareengineer #html #machinelearning #softwaredeveloper #programmers #software #engineering #linux #programmingmemes #artificialintelligence #datascience #computerengineering #cybersecurity #hacking #bhfyp

--

--

Vikram Nayyar CS

Computer Science Student | Digital Content Creator!!