Momma Taught Me To Always Keep My Promises

uh oh

Sadly, in the real world, it’s a little more complicated than that. You can have all the best intentions in the world to fulfill the promise but life happens! For example, if you promised a friend that you would walk their dog after work and something popped up. You usually leave at 6pm and your friend is expecting you at 6:30 but today your boss gave you a few more tasks until you're released into the wild. What happens to our promise?!

Allow me to back up a little bit… In the lovely language of Javascript a promise can be fulfilled ( YAY! WE DID IT ) or rejected ( booo =[ ). We would write it like this:

A promise in action

In our dog walking example, we’re still at work. We can walk the dog when we leave work or if the boss keeps us a tad too long we can apologize to our friend and accept defeat. That means that the dog hasn't been walked yet but at the same time we haven’t broken our promise either. A promise that has neither been fulfilled or rejected is considered pending. Id say think of it like Schrodinger’s Cat but SOMEHOW Quantum Mechanics tends to be more complicated than Javascript. Imagine that!

again… uh oh

The great thing about promises and the reason they are used is because Javascript is considered single threaded.


Javascript starts from the top of your file and executes everything in order while it travels down to the bottom of the file. Yes, we could use chained callback functions to complete our task and lord knows Javascript used to require it but the future is here and we may as well take advantage of it.

aka: callback hell

A promise responds to two commands, then() and catch(). This is what makes promises asynchronous. then() is used on a promise and executed only if the promise has left the pending stage (and is then either rejected or fulfilled). It takes two arguments, one function if the response is successful and another if it is rejected. The catch() call is similar to then(), but works on the specifically on the rejection side meaning it will only be called if the response received is a failure.


Whatever argument that is inside of the then() DOES NOT execute until the promise has either been fulfilled or rejected. It doesn't matter where the code shows up in the file, it could be at the bottom or directly at the top. The great thing about these asynchronous actions is they can be chained together so that you only preform an action only if a specific action before it has been executed. This is helpful because it adds more depth and interactivity to our program.

yoda knows best

In the end, we make a promise to our friend to walk the dog once we leave work. Our promise is unfulfilled until we walk the dog or until we tell our friend we are not going to do it. The fact that it is a promise is important because the function to walk the dog will NOT occur until we leave work (instead of happening at 6:30 regardless). This leads to a happy friend, a happy self and more importantly a happy puppy.