You're not wrong, and the extra step is a bit misunderstood, although important if you want to stream large binary files (I used it in this PWA demo to show a %βblink and you'll miss it).
I have a few unrelated thoughts:
await makes the flow much nicer than Chris' examples
I'm also surprised neither link uses onload, instead using the more confusing onreadystatechange (and this is actually a vote for XHRs).
the lack of support for responseType = 'json' in IE11 actually makes XHRs more confusing, since you just can't safely use that parameter and you have to add a JSON.parse call too
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Using XHR instead of the fetch API is not out of the question. Fetch isn't a 1:1 replacement, and as Chris Ferdinandi points out, requires two functions just to get the data in readable form. (In the first
then()
the data is in a stream.) Another great post by Glenn Stovall demonstrates how to write a small ajax function using XHR without any third party libraries.You're not wrong, and the extra step is a bit misunderstood, although important if you want to stream large binary files (I used it in this PWA demo to show a %βblink and you'll miss it).
I have a few unrelated thoughts:
await
makes the flow much nicer than Chris' examplesonload
, instead using the more confusingonreadystatechange
(and this is actually a vote for XHRs).responseType = 'json'
in IE11 actually makes XHRs more confusing, since you just can't safely use that parameter and you have to add aJSON.parse
call too