Better Programming

Advice for programmers.

Follow publication

Member-only story

Understanding Promises in JavaScript

Gokul N K
Better Programming
Published in
14 min readJul 15, 2018

I am making you a pinky promise that by the end of this post you will know JavaScript Promises better.

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 ?

  1. 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…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Gokul N K
Gokul N K

Written by Gokul N K

Inquisitive, Student, Teacher and a wanna be Story teller. Working on https://learningpaths.io/

Responses (33)

Write a response

Javascript

I hate to be pedantic, but the “S” in JavaScript is supposed to be capitalized.
Here the original Press Release of the language. Notice the spelling. :-)
Scott

24

> But for readability of the code I think it is better to keep them separate.
But the meaning is different when you write them separately.

14