-
-
Save kypflug/f486046ba87076088742212316d0df7d to your computer and use it in GitHub Desktop.
Making a request for a JSON resource with XHR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', url); | |
xhr.responseType = 'json'; | |
xhr.onload = function() { | |
console.log(xhr.response); | |
}; | |
xhr.onerror = function() { | |
console.log("Houston, we've got a problem."); | |
}; | |
xhr.send(); |
Simpler, sure ... and now abort
a fetch ... or show me a download/upload progress .... uh wait ....
My point is that LOC as first reason to ditch anything is a very poor argument.
The two APIs do different things. Comparing them to sell Fetch
is silly because fetch lacks of few things already very simple through XHR and vice-versa, XHR doesn't expose streams so it's inferior.
These are informative points. Showing extra empty lines to demonstrate how much less LOC we have now?
Meh ¯\_(ツ)_/¯
Meh ¯\_(ツ)_/¯
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It was a trivial example. Now do two fetches, with a "both successful" callback and a "something failed" callback.
The point isn't necessarily that it's less code (tho it is, as you conceded), it's that the entire model is simpler.