Question
In the context of this lesson, how is fetch()
different from XMLHttpRequest
?
Answer
Although similar in terms of their purpose, there are some things that fetch()
introduced that made it different from XMLHttpRequest
.
Probably the main difference was that fetch()
uses Promises
. Promises made it easier and more convenient to write code for making asynchronous requests.
In addition, another difference is that with fetch()
, a Promise
is always returned that resolves to the request’s Response
, whether or not it was successful.
Furthermore, when a Response
is retrieved using fetch()
, you are able to use a number of different methods to define the body content and how it should be handled.