0

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);
        });
    })
}
Frizzant
  • 684
  • 1
  • 7
  • 28
  • Why did you comment out the code that would work? You do know that `return` does not work in callbacks right? All `return` does in callbacks is stop processing further code. You need to call a callback to "return" a value like you did for `resolve()` – slebetman Feb 27 '20 at 13:33
  • Definitely use `resolve` and `reject` from the `Promise` callback function instead of `Promise.reject`. Also, where are you getting `.then(result => resolve('Success.'))` this from? What is `resolve` here supposed to do and where is it coming from? – goto Feb 27 '20 at 13:34
  • Actually you can ignore that part. I tried it with resolve() but that didn't work. Let me try it again. – Frizzant Feb 27 '20 at 13:43
  • Oh my...@slebetman @goto1 you are right. That turns out to work. I had the wrong impression of it not working due to my code not doing what I expected it to. You saved me a load of headaches! – Frizzant Feb 27 '20 at 13:46
  • FYI doing `makeRequest().then(...).then(() => resolve('whatever'))` will not work. `resolve` will not be defined in that scope unless it's a callback function that's used for creating a new `Promise` instance, i.e. `new Promise((resolve, reject) => {...})`. Instead, use `makeRequest().then(...).then(() => Promise.resolve/reject('whatever'))` if that's what you're intending on doing, but I can't think of a situation where this would be useful. – goto Feb 27 '20 at 13:58
  • @goto1 this promise chain is within another promise chain. My goal is to resolve the promise which it is nested in at the end of the promise chain. The code does currently work. Although it might too without the resolve(). – Frizzant Feb 27 '20 at 14:49

1 Answers1

0

Turns out my first solution actually was working, but I misunderstood the output.

        if (error) {
            reject(error)
        }
        if (response.statusCode != 200) {
            reject(response.status)
        }
Frizzant
  • 684
  • 1
  • 7
  • 28