Fetch is the new Promise-based API for creating network requests:
fetch('https://www.everythingisawesome.com/')
.then(response => console.log('status: ', response.status));
It makes sense to me - when we initiate a network call, we return Promise, which allows our thread to continue working with another business. When an answer is available, the code inside Promise is executed.
However, if I am interested in the response payload, I do this using the response methods, not the properties:
- ArrayBuffer ()
- blob ()
- FormData ()
- JSON ()
- text()
These methods return promises, and I don't understand why.
fetch('https://www.everythingisawesome.com/')
.then(response => response.json());
.then(entity => console.log(entity.name));
Why handling the response payload returns a promise - I don’t understand why this should be an async operation.
source
share