Before we get started, let’s review some fundamentals necessary to understand Promises and Asynchronous behavior.
Functions are first-class data. This means a function can be an argument to another function and a function can be the return value of another function. This concept allows for the use of callback functions. A callback function, simply, is when a function is passed into another function as an argument yet invoked at a later moment.
Here is how Promises work gathered into a simple analogy I read online. You are hungry. You are trying to decide what to eat for dinner. You know you will eat dinner but you do not know what yet --- this is the Promise; the idea that you will eat but do not know the specific meal. You go out to get tacos. If you go to your favorite taco restaurant and they have not yet sold out of tacos, you will buy the tacos and eat them for dinner at home -- this would be a Promise fulfilled AND an invoked, successful callback. Yet, if your favorite restaurant is sold out of tacos, this would be the Promise rejected. You would then go back home and eat a grilled cheese -- making this the invoked failure callback.
This analogy helps to visualize the concept of Promises. A new Promise is pending and its result value is undefined. A fulfilled Promise is resolved and results in a value and a successful callback. Meanwhile, a rejected (or failed) Promise results in an error which is usually a failure callback. These three Promise statuses: fulfilled, pending and rejected determine the behavior and the result.
The statuses also work alongside Promise’s built-in instance methods: then(), catch() and finally(). If the Promise status is pending, then is fulfilled, then()is executed. If the Promise status is pending then is rejected, catch() is executed. The then() method takes a callback function that executes whenever the promise resolves (or is fulfilled). Likewise, the catch() method takes a callback that executes whenever the promise is rejected. This callback takes an error parameter to catch the error information.