What is a Promise?
Three states of a Promise:
•pending: initial state, no fulfillment or rejection has occurred yet.
•fulfilled: the operation was completed successfully
•rejected: operation failed
Let’s say we go ahead and make a data request to a server, it will be pending until we receive said data. If we get the data from the server our promise object will result as resolved succesfully! On the flip side, if for some reason we don’t get the data we requested, our Promise will be in a rejected state.
One big benefit of using Promises is that we can chain Promises, here in this situation we will utilize using the methods .then() & .catch(). Using chained Promises can be an alternative to using many callback functions, and therefore allow us to avoid callback hell(think way too many function calls, and messy code). Instead, we can attach our callback functions to our Promises, and use .then() to return a fulfilled response and use .catch() to return an unfulfilled response where we will get an error message.
See this Submit Handler function for the use syntax of .then() & .catch()