reqwest icon indicating copy to clipboard operation
reqwest copied to clipboard

.catch doesn't fire if .then fails

Open appsforartists opened this issue 10 years ago • 3 comments

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

appsforartists avatar Oct 21 '14 22:10 appsforartists

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.");
    }
});

wbobeirne avatar Jul 25 '16 20:07 wbobeirne

I haven't touched reqwest in a really long time because fetch is a thing now.

appsforartists avatar Jul 25 '16 21:07 appsforartists

@appsforartists Possibly the best comment on this repository in months. Thanks for the heads up (y)

danhardman avatar Jul 26 '16 11:07 danhardman