Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
Promise who return nothing

I'm trying to use the transaction class from the mssql package of nodejs. So as i read on http://www.dotnetcurry.com/nodejs/1238/connect-sql-server-nodejs-mssql-package, i did the following :

Db.prototype.transaction = function() {
  return new Promise((resolve, reject) => {
    pool.connect().then(conn => {
      let transaction = new mssql.Transaction(conn).begin().then(() => {
        new mssql.Request(transaction).query("SELECT * FROM ParkingSlot").then(() => {
          transaction.commit().then((recordset) => {
            conn.close();
            resolve(recordset);
          }).catch(reject)
        }).catch(reject);
      }).catch(reject)
    });
  });
};

On the caller function, i did db.transaction().then(console.dir); to output any result one console. But nothing happen.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It's because some function of mssql i used on the chains return nothing, so when the promise is pending, he'll work with nothing, as it can be seen on the catch chain in the main call.

Secondly, for using mssql of nodejs with transaction, the connection must be always open (so for my case, in the constructor) and i have to deal with transaction only.

Finally, i don't have to create my request or transaction with a new object. The new instance can be retrieved by ConnectionPool::transaction or ConnectionPool::Request

So here is the final code :

transaction(q) {
    return new Promise((resolve, reject) => {
      let t = connexion.transaction()
      t.begin().then(() => {
        let r = t.request();
        r.query(q).then((recordset) => {
          //console.log(recordset)
          t.commit().then((re) => {
            conn.close();
          }).catch(reject);
          resolve(recordset);
        })
      });
    });
  }

Note that the code syntax is different, because i pass on ES6 class syntax, better readable.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!