A Promise is an object that contains the future value of an asynchronous operation. For example, if you request some data from a server, a promise promises us to receive this data that we can use in the future.
At first, the promise has the status
pending, then it has one of:
fulfilled ("successfully completed") or
rejected (" completed with an error🙅♂️ ").
Pending- The promise is pending if the result is not ready. That is, it is waiting for something to complete (for example, the completion of an asynchronous operation).
Fulfilled- Promise resolved if result is available. That is, something completed its execution (for example, an asynchronous operation) and everything went well.
Rejected- Promise was rejected if an error occurred during execution.
Promise object is created using the new keyword and its own constructor.
The Promise constructor takes one argument, a callback, also known as an execution function⚙️, which takes 2 callbacks,
The executive function is executed immediately after the promise is created. A promise is made fulfilled by calling
resolve and rejected by calling
reject take one argument, which can be a string, number, boolean expression, array, or object.
To provide a function with promises functionality, you just need to return a
Promise object in it:
Thanks goes to these wonderful people (emoji key):
🐛 🎨 🖋
🖋 🐛 🎨