I stumbled upon the following code (reference):
function handleErrors(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}
fetch("http://httpstat.us/500")
.then(handleErrors)
.then(response => console.log("ok") )
.catch(error => console.log(error) );
I would have expected something like this:
then((response) => { handleErrors(response) })
How is it possible that response
is successfully being passed to the reference of handleErrors()
?
Does it have something to do with promises or is this a javascript pattern that I don't know about?