javascript - Unhandled promise rejection even though I do handle it - Stack Overflow

I wrapped a request-promise-native call in a function that returns a promise.import request from '

I wrapped a request-promise-native call in a function that returns a promise.

import request from 'request-promise-native';
function requestWrapper(url) {
    return request(url)
        .then(data => {...})
        .catch(err => Promise.reject(err));
}

Simple right?

Now I'm using this function, and thenworks ok, but catch never catches the Promise.reject:

requestWrapper('')
    .then(data => {...})
    .catch(err => console.log(err));

I never get to the call's catch! If I change the return statement in the catch of requestWrapper to this:

.catch(err => err)

or even this:

.catch(err => Promise.resolve(err))

to return a resolve, I get the error stack on the then of the requestWrapper call just as expected.

And node shouts:

(node:24260) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): ...

I wrapped a request-promise-native call in a function that returns a promise.

import request from 'request-promise-native';
function requestWrapper(url) {
    return request(url)
        .then(data => {...})
        .catch(err => Promise.reject(err));
}

Simple right?

Now I'm using this function, and thenworks ok, but catch never catches the Promise.reject:

requestWrapper('http://some-url.')
    .then(data => {...})
    .catch(err => console.log(err));

I never get to the call's catch! If I change the return statement in the catch of requestWrapper to this:

.catch(err => err)

or even this:

.catch(err => Promise.resolve(err))

to return a resolve, I get the error stack on the then of the requestWrapper call just as expected.

And node shouts:

(node:24260) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): ...
Share Improve this question asked Jan 23, 2018 at 9:21 MosheMoshe 5,1697 gold badges36 silver badges61 bronze badges 4
  • did you tried error callback in then? – zabusa Commented Jan 23, 2018 at 9:24
  • 1 try .then(data => {...}, err => console.log(err)) – pathurs Commented Jan 23, 2018 at 9:25
  • @zabusa the then works fine. I need to catch errors, it does reach the catch statement in requestWrapper, then problem is how this is handled when I call the function. – Moshe Commented Jan 23, 2018 at 9:25
  • 1 Maybe don't catch it in the requestWrapper, only when you call the function? – Hendry Commented Jan 23, 2018 at 9:27
Add a ment  | 

2 Answers 2

Reset to default 6

Ok thanks to @pathurs and @Hendry I got what I needed.

First of all, the right way is what @Hendry suggested which was to not handle the catch in the wrapper, rather in the function call. This is the way I went:

function requestWrapper(url) {
    return request(url)
        .then(data => {...})
}
requestWrapper('http://some-url.')
    .then(data => {...})
    .catch(err => console.log(err));

Great! But apparently, @pathurs way also works:

function requestWrapper(url) {
        return request(url)
            .then(data => {...})
            .catch(err => Promise.reject(err));
    }
    requestWrapper('http://some-url.')
        .then(data => {...}, err => console.log(err));

I don't understand if that's a native feature, or some addition to request library: https://github./request/request-promise#thenonfulfilled-onrejected

if you are not getting that then do this workaround.

In order to handle rejections the following new events will be fired on process:

Event 'unhandledRejection':

Emitted whenever a possibly unhandled rejection is detected. This event is emitted with the following arguments:

reason the rejection reason of the promise susprected in having an unhandled rejection p the promise suspected in having an unhandled rejection itself.

process.on('unhandledRejection', function(reason, p){
    console.log("Possibly Unhandled Rejection at: Promise ", p, " reason: ", reason);
    // application specific logging here
});

For more info Unhandled Rejection

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743730023a4497273.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信