hretic
hretic

Reputation: 1085

reject doesn't break the promise operation

i have 2 insert query that i need to chain to each other by transaction cuz sometime one of those query may fail (thats by design), i use promise to handle what happens next

new Promise(function(resolve , reject ) {
        pool.getConnection(function(err, connection) {
            connection.beginTransaction(function (err) {
                if (err) {
                    reject(err.sqlMessage);
                }
                connection.query('INSERT INTO main_table  SET ?', main_table_data, function (err, result) {
                    if (err) {
                        connection.rollback(function () {
                            reject(err.sqlMessage)
                        });
                    }

                    connection.query('INSERT INTO child_table  SET ?', child_table_data, function (err, result) {
                        if (err) {
                            connection.rollback(function () {
                                reject(err.sqlMessage)
                            });
                        }
                        connection.commit(function (err) {
                            if (err) {
                                connection.rollback(function () {
                                    reject(err.sqlMessage)
                                });
                            }
                            resolve(result.insertId);
                        });
                    });
                });
            });
        });
    })

.then(function (ID) {

        console.log('/////////SUCCESS/////////////');
        console.log(ID);

})
.catch(function (error) {

    console.log('/////////ERROR/////////////');
    console.log(error);

});

the problem is i cant stop it from breaking the whole code , it execute the catch part

.catch(function (error) {

    console.log('/////////ERROR/////////////');
    console.log(error);

});

but the code inside the promise will go on instead of breaking ... this part specifically

  resolve(result.insertId);

this is my output

/////////ERROR/////////////
invalid wallet
C:\wamp64\www\expbet\serv\node_modules\mysql\lib\protocol\Parser.js:437
      throw err; // Rethrow non-MySQL errors

which is the output im expecting in the catch , but right after im getting

TypeError: Cannot read property 'insertId' of undefined

cuzed by this code

  resolve(result.insertId);

why its still running resolve code if its been already rejected ? what im doing wrong

that second error breaks the whole node code , if thats not how reject works and i need to throw an error how can i do it without breaking the whole code and catch it in the promise catch ?

Upvotes: 0

Views: 485

Answers (1)

Roamer-1888
Roamer-1888

Reputation: 19288

Why its still running resolve code if its been already rejected?

Take a look at your if(err) { reject(err.sqlMessage); } clauses. None of them has an else {} clause therefore execution will continue as if there had been no error.

What im doing wrong?

You are assuming that the occurrence of an error and/or calling connection.rollback() and/or calling reject() in the nodebacks will inhibit further execution of code. This is not the case.

Upvotes: 1

Related Questions