Discussion:
throw vs promise reject
Guru Partap Khalsa
2018-09-17 10:44:43 UTC
Permalink
What is the reasoning for XMLHttpRequest to "throw" rather than rejecting a
promise? It almost seems sarcastic to ask why a stack trace is helpful when
the network times out.

Thank you for any insight
Boris Zbarsky
2018-09-17 21:43:03 UTC
Permalink
Post by Guru Partap Khalsa
What is the reasoning for XMLHttpRequest to "throw" rather than
rejecting a promise?
The API predates promises. If you want a Promise-based fetching API,
you should use fetch().

-Boris
Tab Atkins Jr.
2018-09-20 18:45:25 UTC
Permalink
On Mon, Sep 17, 2018 at 2:38 PM Guru Partap Khalsa
What is the reasoning for XMLHttpRequest to "throw" rather than rejecting a promise? It almost seems sarcastic to ask why a stack trace is helpful when the network times out.
Tangential to what Boris just said, note that all promises rejected by
the platform get rejected with an Error, so you still get a stack
trace either way. ^_^

(And this is the generally correct practice, as `await` turns a
rejection into a thrown error.)

~TJ
Boris Zbarsky
2018-09-20 18:57:17 UTC
Permalink
Post by Tab Atkins Jr.
Tangential to what Boris just said, note that all promises rejected by
the platform get rejected with an Error, so you still get a stack
trace either way. ^_^
Sort of. A bunch of them get rejected with a DOMException instead.
That said, those have a stack as well, in browsers though not in the spec.

-Boris
Guru Partap Khalsa
2018-09-21 00:00:49 UTC
Permalink
Thanks for your direct and insightful answer.

Loading...