Momma Taught Me To Always Keep My Promises
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?!
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.
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.