Which should you use? XHR or Fetch?
Why use XHR
XHR uses a callback pattern which you are familiar with. You may find it easier to understand.
XHR has two more features compared to Fetch (at the time of writing):
One. You can track the progress of a request with XHR, but you cannot track the progress of a request with Fetch.
Two. You can cancel both XHR and Fetch requests, but canceling requests with Fetch isnβt well supported across browsers yet.
Why use Fetch
Fetch uses JavaScript promises. Promises are powerful. They let you handle complex requests easily compared to events and callbacks. Youβll learn more about promises in the next module.
Fetch is actively worked on. Itβll get more features in future. It can be used in newer technologies like Service Workers as well.
Wrapping up
Use XHR if:
- You need to track the progress of a request
- You need the ability to cancel requests
Otherwise, use Fetch.
Welcome! Unfortunately, you donβt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson