What Is the Promise API?
As mentioned earlier, the Promise API helps manage asynchronous operations when making network requests to the server. It is a JavaScript object that contains the future values of an async operation, either a resolved value or a reason if it was rejected. A promise has three states:
Resolved
: The action/operation has succeeded.Rejected
: The action/operation is failed.Pending
: The action/operation is pending; it's not fulfilled or rejected yet.
We have seen how to fetch data using the promise-based Fetch API in Chapter 14; Fetching Data by Making API Requests, let's review that again and see what values are getting returned.
Let's add the following code in the Chrome Console window:
fetch('https://jsonplaceholder.typicode.com/posts/1') Â Â .then(function(res) { Â Â Â Â return res.json(); Â Â }).then(function(data) { Â Â Â Â console.log(data); Â Â ...