0

I have nested promises when call is done the inner promise return a reject and the outer promise catch that reject but does not return it and the process ends.

function simpleExecute(sql, bindParams, user_ldap) {
    return new Promise(function (resolve, reject) {
        var options = {
            user: user_ldap,
            outFormat: oracledb.OBJECT
        }
        return getConnection(options)
            .then(function (connection) {
               return execute(sql, bindParams, options, connection)
                    .then(function (results) {
                        resolve(results);
                        process.nextTick(function () {
                            releaseConnection(connection);
                        });
                    }, (rej) => {
                        return reject(rej); <----- Reject here
                    }).catch(function (err) {
                        if (err) {
                            console.log(err);
                            return reject(err);
                        }
                        process.nextTick(function () {
                            releaseConnection(connection);
                        });
                    });
            }, (rej) => {
                return reject(rej);
            });
    }, (rej) => {
        return reject(rej);
    }).catch(function (err) {
        return reject(err); <---- catch here and no return!!
    });
}

and my caller is

await ids.then(res => {
    console.log(res);
}, rej => {
    console.log(rej);
}).catch(err => {
    console.log(err);
});

in the caller function, I can not get any response from the promise when its rejected. any help is appreciated.

CodeRed
  • 905
  • 1
  • 6
  • 24
MuhanadY
  • 752
  • 1
  • 12
  • 40
  • Avoid the explicit Promise construction antipattern, you're using it everywhere and it's not helping anything here, just return each Promise alone and catch in the caller and the problem be solved – CertainPerformance Sep 27 '19 at 09:10
  • @CertainPerformance can you please explain more – MuhanadY Sep 27 '19 at 09:13
  • Unless a particular `.catch` level can actually handle the error, there's no point to it, it'll just make the code longer and more confusing. Just `return` each created Promise instead, without creating an extra one with `new Promise` – CertainPerformance Sep 27 '19 at 09:15
  • @CertainPerformance you are right after I simplify the code everything worked well. thanks. var options = { user: user_ldap, outFormat: oracledb.OBJECT } return getConnection(options).then(function (connection) { return execute(sql, bindParams, options, connection) }); – MuhanadY Sep 27 '19 at 11:46

0 Answers0