Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node 8.6 javascript promises: UnhandledPromiseRejectionWarning

I am having an error: (node:6186) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): threep (node:6186) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. -------- ones ========= two CaughtCathchError threep (node:6186) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1) I am consuming my 3 promise functions in nested order. p1,p2,p3- are my promise functions as shown below. i tried adding promise reject in all p1,p2,p3 functions as well but its still the same

enter code here
var p1 = new Promise(function (resolve, reject) {
    setTimeout(function () {
       // resolve('ones')
                resolve('ones')
    }, 9000)
})
var p2 = new Promise(function (resolve, reject) {
    setTimeout(function () {
        // throw new Error('eeeee');
        //reject('two')
    resolve('two')
    }, 1000)
})
var p3 = new Promise(function (resolve, reject) {
    setTimeout(function () {
        reject('three')
    }, 4000)
})

p1.then(function(result){
    console.log("--------", result)
        // return p2.then(function(res){console.log(res)}).catch(function(err){console.log(err)})
        return p2
}).then(function(p2result){
    console.log("=========", p2result)
    return p3;
}).then(function(p3result){
    console.log('*********', p3result)
}).catch(function(err){
    console.log("CaughtCathchError", err)
})
like image 467
JAVA Programmer Avatar asked Aug 07 '26 21:08

JAVA Programmer


2 Answers

p3 is a standalone Promise with no .catch. So, when p3 gets rejected, you get a UnhandledPromiseRejectionWarning. Even if p3 is consumed later in a Promise chain that has a proper catch, p3 itself does not have a catch.

Instead of p3, you might use a function that returns a Promise, and ensure that all calls to that function are caught:

var p1 = new Promise(function (resolve, reject) {
    setTimeout(function () {
       // resolve('ones')
                resolve('ones')
    }, 1000)
})
var p2 = new Promise(function (resolve, reject) {
    setTimeout(function () {
        // throw new Error('eeeee');
        //reject('two')
    resolve('two')
    }, 1000)
})
var getp3 = () => new Promise(function (resolve, reject) {
    setTimeout(function () {
        reject('three')
    }, 1000)
})

p1.then(function(result){
    console.log("--------", result)
        // return p2.then(function(res){console.log(res)}).catch(function(err){console.log(err)})
        return p2
}).then(function(p2result){
    console.log("=========", p2result)
    return getp3();
}).then(function(p3result){
    console.log('*********', p3result)
}).catch(function(err){
    console.log("CaughtCathchError", err)
})

If you need to initialize p3 immediately, then put a catch after the p3 itself.

like image 51
CertainPerformance Avatar answered Aug 09 '26 13:08

CertainPerformance


Node.js promise implementation expects rejected promise to be chained with catch(...) or then(..., ...) synchronously, otherwise PromiseRejectionHandledWarning appears. Unhandled promise rejections may result in exception in future.

Usually a rejection should be treated as any other error, so it's preferable for it to be an instance of Error and not plain string. Then it could be treated as such:

class ExpectedRejection extends Error {}

var p3 = new Promise(function (resolve, reject) {
    setTimeout(function () {
        resolve(new ExpectedRejection('three'))
    }, 4000)
})

...
.then(function(p3result){
  if (p3result instanceof ExpectedRejection)
    throw p3result;

  console.log('*********', p3result)
}).catch(function(err){
    console.log("CaughtCathchError", err)
})

ExpectedRejection class is specific and possibly not needed; could be replaced with Error everywhere because Error resolutions are uncommon.

This solution is not very common because if promises are created at the same time, they don't depend on each other and usually can be handled with Promise.all:

Promise.all([p1, p2, p3])
.then(function(p1result, p2result, p3result){
    console.log('*********', p3result)
}).catch(function(err){
    console.log("CaughtCathchError", err)
});

Notice that since p1, p2 and p3 are delays that are created at the same time, resulting catch will be triggered after 9 seconds (maximum duration of a delay) in both cases.

like image 35
Estus Flask Avatar answered Aug 09 '26 11:08

Estus Flask



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!