I can not get the .catch
to work. I have tried multiple solutions like throw
and the uncommented reject()
.
I am puzzled why it is not working, since I tested https://stackoverflow.com/a/47803880/7262739 which seems to work exactly the same, but resulting in a working .catch
.
I get this error: Unhandled rejected promise: 404 Not Found
as if I didn't handle the rejection.
simplified version of the code:
makeRequest(theUrlOne)
.then(result => makeRequest(theUrlTwo))
.catch(result => console.log(result)) // not working, but not the problem
.then(result => resolve('Success.'))
.catch(result => resolve('Failed.')) // the problem-child
function makeRequest(url, method, datatype, timeout) {
return new Promise(function (resolve, reject) {
http.simpleRequest({
'method': method || 'GET',
'url': url,
'dataType': datatype || 'json',
'timeout': timeout || 6000,
}, function (error, response) {
if (error) {
return Promise.reject(error)
// reject(error)
}
if (response.statusCode != 200) {
return Promise.reject(response.status)
// reject(response.status)
}
let parsed = JSON.parse(response.data)
resolve(parsed);
});
})
}
Turns out my first solution actually was working, but I misunderstood the output.
if (error) {
reject(error)
}
if (response.statusCode != 200) {
reject(response.status)
}