Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

249
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda