0

From https://javascript.info/promise-chaining :

let promise = fetch(url);

This makes a network request to the url and returns a promise. The promise resolves with a response object when the remote server responds with headers, but before the full response is downloaded.

Just being curious, why separates response headers and body into 2 steps/promises? If I remember correctly, traditional XMLHttpRequest does behave like this. Since the body always comes after header, does it make a difference to separate this complete process?

Rick
  • 7,007
  • 2
  • 49
  • 79
  • Maybe you only need to get the headers. Or you need to get the body only if headers are set to something. I.E. if headers says 200 (OK) then wait for body, otherwise throw error. – GrafiCode Feb 11 '20 at 17:49
  • 1) So that you can access the headers without waiting for the body 2) so that you can decide what to do with the body depending on the headers 3) so that you can also stream the response body - have a look at all the methods a `Response` has – Bergi Feb 11 '20 at 18:13

0 Answers0