0

I read that it is better to use request-promise instead of request, because there is better error handling.

In request:

request('http://www.google.com', function (error, response, body) {
  if (error) console.log(error);
  //operations...
});

In request-promise:

rp('http://www.google.com')
    .then(function (htmlString) {
        // operations..
        // operations..
        testerror; // line 5
    })
    .catch(function (err) {
        console.log(err); // line 8
    });

But if I have error in line 5 then console.log show that error is line 8 then if it isn't from request-promise just from my code then I don't know what happened.

How in request-promise I can show error line - 5, not 8?

zafuhege
  • 1
  • 1
  • "then" takes a second parameter, a function which is where you end up if the promise is rejected. Did you try that? – yBrodsky Jul 11 '17 at 20:10
  • @yBrodsky that is a deprecated approach. Promises should use `then()` and `catch()` for resolve and reject respectively. – peteb Jul 11 '17 at 20:16
  • 2
    Sounds like `console.log` prints the location of the log call? You'll want `console.log(err.stack)` in that case – Bergi Jul 11 '17 at 20:16
  • @peteb [No it's not deprecated](https://stackoverflow.com/q/24662289/1048572). It's just two different things, and one should employ the appropriate one. – Bergi Jul 11 '17 at 20:17
  • @Bergi thanks for the link to explain the exact difference. Not to start a separate conversation but, cursory look at your answer in that question says, that `catch()` is going to handle the whole chain and any errors that occur within it. Whereas `then(success, fail)` will handle the error for that specific step in the promise chain? Am I understanding that correctly? – peteb Jul 11 '17 at 20:18
  • @peteb `promiseChain.then(onFulfill, onReject)` will handle any errors from the whole `promiseChain` as well. It will not handle errors from `onFulfill`. – Bergi Jul 11 '17 at 20:23
  • @Bergi ah okay, so it will handle other errors within the chain just not any that occur with `onFulfill`. I guess using the Bluebird Antipattern docs as blanket Promise Antipatterns isn't a good idea >. – peteb Jul 11 '17 at 20:24
  • https://jsfiddle.net/jht2vh1u/1/ Here are the two things that can happen. The function rejects the promise and then an error that's caught. When the promise is rejected, the result is the second param of the "then" – yBrodsky Jul 11 '17 at 20:32

0 Answers0