V
V
vetsmen2018-03-04 14:19:34
JavaScript
vetsmen, 2018-03-04 14:19:34

How to execute a certain code after a function call?

There is a code:

exports.prepareTransaction = () => {
    return new Promise((resolve, reject) => {
        pool.getConnection((err, connection) => {
          if (err) throw err;

          const beginTransaction = promisify(connection.beginTransaction).bind(connection);
          const query = promisify(connection.query).bind(connection);
          
          const commit = promisify(connection.commit).bind(connection);
          const rollback = promisify(connection.rollback).bind(connection);

          resolve({beginTransaction, query, commit, rollback});
        });
    });
};

I work with it like this:
let transaction = await connection.prepareTransaction();	

try {
  await transaction.beginTransaction();
  ...
  await transaction.commit();
}

I need that when transaction.commit() is called, after it is executed, connection.release() is executed inside my promise, that is, I must somehow pass the connection context to my promise. It should turn out something like:
const commit = promisify(connection.commit( () => connection.release() )).bind(connection);

Only it doesn't work. How to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
qwerty, 2018-03-09
@Bapen1k

Why can't you write
await connection.release(); after await transaction.commit();?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question