I'm relatively new to Promises in js and am having trouble understanding why the following block of code does not run my catch function when the server responds with a 401 unauthorized.
loginUser(email, password).then((token) => {
console.log("in then")
//ipcRenderer.send('login-success', token)
}).catch(err => {
console.log("in catch") //not running
})
The loginUser function:
function loginUser(email, password) {
let body = { email: email, password: password }
return fetch('http://localhost:3000/api/v1/sessions', {
method: 'POST',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' }
}).then(response => {
return response.json().then(json => {
console.log(response.ok) // false
return response.ok ? json : Promise.reject(json)
}).catch(err => {
console.error(err)
})
})
}
Any help at all would be appreciated. Cheers