Please see this minimum example:
I have data like this:
const testObject = { test: 'foo' };
And my main function is this:
Cause error
// This cause error
function handleResponse(response) {
return response.json().then(Promise.reject); // Please notice this line
}
try {
await handleResponse({
json: () => Promise.resolve(testObject),
});
} catch (err) {
console.log(err);
// => TypeError: PromiseReject called on non-object
}
And this one works:
Correct
// This works
function handleResponse(response) {
return response.json().then((res) => Promise.reject(res)); // Please notice this line
}
try {
await handleResponse({
json: () => Promise.resolve(testObject),
});
} catch (err) {
console.log(err);
// => {test: "foo"}
}
Why is this happening? What did I missing?