Promises and Inception

Josh Strzeszkowski
Sep 3, 2018 · 5 min read

Warning: The following contains spoilers for Inception

Callback hell is real. If you sin while coding, you will be destined to get lost there. If you don’t want to spend eternity in the realm of Javascript Hades, repent and I PROMISE you will be saved.

Ok, ok, callback hell is not that bad. What it is in actuality, is a maze of tangled callbacks that can be a nightmare to read and debug. Fortunately, we have a workaround in Javascript called promises to help us mitigate and prevent us from falling into the depths of callback hell.

Before we can discuss callbacks and promises, we must first briefly describe synchronous and asynchronous functions. Synchronous functions occur sequentially, that is, their events occur one after the other in order. For example, The Dark Knight is a synchronous movie. It’s events occur in order.

The Dark Knight

Asynchronous functions are non-sequential. They occur out of order. Inception is a perfect example of an asynchronous movie. The very first scene, when Leonardo DiCaprio’s Cobb washes up on the beach and is taken to an aged Saito (Ken Watanabe), occurs at the end of the movie. Then the film reverts to the beginning and intersperses flashbacks throughout.

Callbacks and Promises are both asynchronous. Callbacks are functions that are passed to other functions and are called with the result of an action. Callbacks can be chained and when this happens, it is generally referred to as callback hell.

The above code contains several nested callback functions. This code is ugly and messy. While we can sort of tell what is going on(we are attempting to complete 3 tasks: dream, incept, and go_home), it can be difficult to interpret and even more difficult to debug. I mean, just look at all of those parenthesis and braces. You may have to spend far too much time just matching those up. Think of callback hell as Javascript’s version of Limbo.

Cobb and his wife Mol (Marion Cotillard) spend over 50 years in limbo (only a few hours in non-dream time). They get lost and can’t find their way out. Mol never even thinks she leaves. Imagine if callback hell were like? We’d give up coding and never return.

Fortunately, we can use promises to escape. A promise is an object that produces a value that will be completed after specific actions are taken. Promises are designed to mitigate the use of callbacks. They do not get passed to functions, easing the readability and debugging time. Additionally, promises can be attached to each other to view a sequence of events.

Promises take 2 parameters: resolve and reject. Resolve is invoked when the function fires correctly, whereas reject is invoked if the function fails.

This code is much more readable. We (or probably Cobb) are first entering a dream, then we are incepting an idea, then we get to go home. The ‘.then’ keyword allows us to chain promises (function A happen, THEN function B happens). We use the ‘.catch’ keyword to literally catch potential errors. In this case, Cobb’s totem (way to see if he’s still in the dream) is a spinning top that never stops spinning in the dream. So if the top keeps spinning, we’re still in the dream and the promise is rejected.

An easy way to think of a promise is with the deal that Saito makes with Cobb. Saito promises to resolve Cobb’s legal issues so he can return home to his kids, if Cobb helps Saito to get his rival businessman, Fischer, played by Cillian Murphy, to split his company. From there, the story asynchronously plays out in a series of events with nested dreams levels that culminate with Fischer finding a pinwheel in his dying father’s safe ‘proving’ to Fischer that his father cared about him and wanted him to be his own man.

Cobbs rescues Saito from limbo as the movie catches up to its opening scene. Saito then resolves his promise by getting the charges against Cobb dropped so he can legally re-enter the United States and come home to his kids. However, our catch occurs in the very last scene of the movie, when Cobb spins the top to see if he is still dreaming. However, the movie ends before we find out if the catch is invoked.

To conclude, promises are a way to mitigate the need for and use of callbacks. We don’t want to end up in limbo like Cobb, Mol, and Saito. We want to use promises to make easier to read code. Honestly though, Inception is an incredibly difficult movie to understand. We don’t want to spend eons debugging callback hell when we need to watch and re-watch Inception to fully understand it.

Now, go add promises to your code and watch Inception. Then we can argue as to whether or not the top stopped spinning or if it was even relevant.