Explain JavaScript Promises like I am five.

I am starting to hate JavaScript. Before I blow my head with a gun, someone, please tell me how to work with JavaScript Promises.


Did you find this post useful? Show some love!

You know what a callback is, right?

Let's take the following example:

var c;
setTimeout(function() {
  if(c) c(true);
}, 1000);

function onTimeout(callback) {
  c = callback;

function myCallback(v) {
  if(v) console.log("Called!");


The onTimeout function takes a callback which will be stored for later use. The later use is the timeout that triggers after 1 second.

Problem here? When we call onTimeout too late, the timeout could be already have triggered, so we would add the callback, but nothing happens.

Solution? Promises!

How do they work? Easy, they are monads :D

Joke aside...

They are objects you can attach your callbacks to.

var p = new Promise(function(resolve, reject) {
  setTimeout(function() {
  }, 1000);

function myCallback(v) {
  if(v) console.log("Called!");


Here we create a Promise object instead, often you get the final object from a function as return-value, so you don't have to think about how they are created, you often just have to call the then method on it and pass a callback.

Here we create one on our own, it takes a callback that receives two functions.

  • resolve has to be called when the asynchronous process finishes successfully.
  • reject has to be called when it fails.

As you may notice, this callback isn't the callback from the previous example. It's a wrapper to make a promise from a non-promise/callback function.

Anyway, promises bring you nice things:

You can call then as often as you like, the callbacks get all called when the promise succeeds.

p.then(function() {console.log("Hello");})
p.then(function() {console.log("World");})

You can call then whenever you like, the callbacks get called right away if the promise has succeeded in the past.

setTimeout(function() {
  p.then(function() {console.log("Hello");})
}, 9999);

You can chain promises with then to make asynchronous calls based on other asynchronous calls.

var fetchPromise = p.then(function(v) {
  return fetch(SOME_URL + "?value=" + v);

var jsonPromise = fetchPromise.then(function(response) {
  return response.json();

var someDataPromise = jsonPromise.then(function(json) {
  return json.someData;

someDataPromise.then(function(someData) {

The then method returns a new promise. The fetch function also returns a promise.

If we pass a callback to then that returns a promise itself, then will return this new promise.

In our example, the first then call returns what fetch returns, because fetch returns a promise.

The json method of our response object also returns a promise, so then will return that promise.

In the third then callback, we return a value from that parsed JSON, so then creates a new promise for us that resolves to that value. This allows us to chain synchronous and asynchronous calls with then.

So this example could be written a bit shorter:

p.then(function(v) {
  return fetch(SOME_URL + "?value=" + v);
.then(function(response) {
  return response.json();
.then(function(json) {
  return json.someData;
.then(function(someData) {

I hope this helps, if you got any questions, ask away :)

Hey, thanks for explaining. This answer helps a lot.

Men, thanks for the promises information.

How is this not a blogpost on itself :D

One little thing I'll add because I often see beginners get this wrong: (you wrote this but I still want to really emphasize this)

Don't have nested then blocks! then is a special function because it can return either a new promise or a concrete value. If you want to perform two promises sequentially, simple return the next promise from the then block.

Instead of

getUsers().then((users) => {
    getUserData(users[0]).then((user) => {
        // ...

You can write

  .then((users) => return getUserData(users[0]))
  .then((user) => /* ... */)

Which is much more readable. In functional terms, then is both bind and map, depending on the return value.

Think of a regular JS array as an envelope. You put one (or more) values into the envelope, and it gives you extra behaviour like map and reduce

const a = 1
      a.map(x => x * 2) // error
      a.reduce((x, y) => x + y, 0) // error

const wrapped = [a]
      wrapped.map(x => x*2) // [2]
      wrapped.reduce((x, y) => x + y, 1) // 3

In fact, we can say that JavaScript arrays are envelopes that have the special property of including multiple values.

Promises are similar. They are envelopes just like arrays are, but the special property they have is they handle future/potential/asynchronous values.

const f = x => new Promise((res, rej) => x ? res(x) : rej(x))

        .then(x => x * 2)
        .then(console.log) // 4

        .then(x => x * 2) // this function never runs
        .catch(x => console.error(`promise rejected for ${x}`)) // promise rejected for null

This is especially useful for managing asynchronous problems. By using a functional approach, Promise flows can be written in a top-down, left-to-right manner:

const handleAsText = response => response.text()
const unsafeDocumentWrite = text => {

fetch('google.com') // fetch API returns a Promise<Response>

I get it now...

In fact, we can say that JavaScript arrays are envelopes that have the special property of including multiple values.
Promises are similar. They are envelopes just like arrays are, but the special property they have is they handle future/potential/asynchronous values.

Thanks a lot!


The non-eli5 answer is that Promises are (not 100% but basically) monads that encode asynchronous behaviour

I've written a blog post explaining Promises. Hope it can help you in understanding.

I would also love if you could go through and point out anything that can be improved!


Hey, thanks a lot. I will check it out. :)

Here's another thread where I answered a similar question: dev.to/shubhambattoo/explain-x-lik...

Promises can be confusing! Don't expect yourself to understand them right away.

Hey, thanks for sharing.
And yes, promises are hard to adapt.

Maybe not 5 exactly, but I did the most basic explanation right here: dev.to/ardennl/about-promises-and-...

Once you have graduated from promise school, you can check out some helpful tips I wrote for promises dev.to/kepta/promising-promise-tip...

Hey, thank you. :)
I will look it up.

Classic DEV Post from Sep 17

Who's looking for open source contributors? (September 17 edition)

Find something to work on or promote your project here. Please shamelessly pro...

Chinmay Joshi
Full Stack Software Engineer from Pune, India. Currently living in Kuala Lumpur, Malaysia.

Do you write code almost every day?

Join dev.to ❤️