reqwest
reqwest copied to clipboard
.catch doesn't fire if .then fails
Reqwest's then
implementation is faulty. It should be wrapped in a try/catch
and pass the error onto the promise's catch
method, per the spec.
For comparison, here's the correct behavior (verified using Node's in-built promises):
> var promise = new Promise(function (resolve, reject) {global.resolve = resolve})
undefined
> resolve
[Function]
> promise.then(function () { throw new Error("hi") }).catch(function(error) { console.log("caught " + error)})
{}
> resolve()
undefined
> caught Error: hi
Confirming this isn't working, and is particularly frustrating for if you want to save code and just trigger the catch
case in your then
function. I.e.
reqwest({
url: ajaxPath,
data: ajaxParams,
})
.then((res) => {
if (res && res.data) {
hellYeahData(res.data);
}
else {
throw new Error(res);
}
})
.catch((err) => {
if (err && err.message) {
ohNoShowError(err.message);
}
else {
ohNoShowError("Something went wrong, try again.");
}
});
I haven't touched reqwest
in a really long time because fetch
is a thing now.
@appsforartists Possibly the best comment on this repository in months. Thanks for the heads up (y)