Hacker News .hnnew | past | comments | ask | show | jobs | submitlogin

    if (xhr.readyState !== 4) return;
is gross and stateful. Certainly you can learn it and use it but

    .then((res) => res.json())
is much more declarative.


And if you forgot to call xhr.send() at the end. You screw up and your callback is never called. fetch doesn't even allow you to make this kind of mistakes.


It allows you take make other mistakes, such as using the wrong type for POST data resulting in "[object Object]" and similar gibberish being sent to the server.

Not saying XMLHttpRequest API's is brilliant, but fetch's API also doesn't exactly fill me with joy.


I think the key is the comparative "-er" in "cleaner" and "simpler." Fetch may not be the cleanest or simplest possible solution, but in my opinion it is cleaner and simpler. XMLHttpRequest is good. Fetch is better.


And I have the entirety of an XMLHttpRequest in my head anyway, that xhr.send(fd); is where you included your form data…

These strange notions of burden and beauty…


You don't need to check readyState; onload is only called when the readyState switches to 4.


At least fetch is a viable replacement for jQuery.ajax and the syntax is quite similar.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: