Member-only story
Understanding Promises in JavaScript
An in-depth look at creating and handling Promises

I have a kind of love-hate relationship with JavaScript. Nonetheless, it has always intrigued me. Having worked on Java and PHP for the last ten years, JavaScript seemed different but intriguing. I did not get to spend enough time on it and have been trying to make up for it lately.
Promises was the first interesting topic that I came across. Time and again I have heard people saying that Promises “saves you from Callback hell”. Well, that might be a pleasant side-effect, but there is more to Promises than that. This is what I have been able to figure out up until this point.
Background
Working in JavaScript it can be a little frustrating at first. You’ll hear people say that JavaScript is a synchronous programming language, then others will claim that it is asynchronous. You hear blocking code, non blocking code, event-driven design pattern, event life cycle, function stack, event queue, bubbling, polyfill, babel, angular, reactJS, vue JS and a lot of other tools and libraries. Fret not — you are not the first. There is a term for it: JavaScript Fatigue. This tweet captures it well:
JavaScript is a synchronous programming language. But thanks to callback functions we can make it function like an asynchronous programming language.
Promises, in layman’s terms
Promises in JavaScript are similar to the promises you make in real life, so let’s look at promises in real life.
This is the dictionary definition of a promise:
promise : noun : Assurance that one will do something or that a particular thing will happen.
So what happens when someone makes a promise to you ?
- A promise gives you an assurance that something will be done. Whether they (who made the promise) will do it themselves or they get it done by others is immaterial. They give you an…