Javascript 我如何在完成knex交易时执行操作?

Javascript 我如何在完成knex交易时执行操作?,javascript,transactions,bluebird,knex.js,Javascript,Transactions,Bluebird,Knex.js,我有一个knex事务,我正在其中执行操作,并将事务引用传递给其他方法,在这些方法中,我希望添加事务完成后将发生的操作 以蓝鸟承诺为例,我想要这样的东西 function a () { return knex.transaction(function(trx) { trx("blah").select().where("fu","bar") .then(function(res) { b(trx); }).then(trx.commit) .cat

我有一个knex事务,我正在其中执行操作,并将事务引用传递给其他方法,在这些方法中,我希望添加事务完成后将发生的操作

以蓝鸟承诺为例,我想要这样的东西

function a () {
  return knex.transaction(function(trx) {
    trx("blah").select().where("fu","bar")
    .then(function(res) {
       b(trx);
    }).then(trx.commit)
    .catch(trx.rollback); 
  }
}

function b(trx) {
  return trx("blah").select().where("fu","bar")
  .then(function(res) {
      // This is where I want to add code to occur after the trx commits
      trx.then(function(){//Do stuff after trx commits})
  }
}

解决方案很简单,我只是忽略了它:存储事务承诺并将其传递给内部方法,如下所示:

function a () {
  trxPromise = knex.transaction(function(trx) {
    trx("blah").select().where("fu","bar")
    .then(function(res) {
       b(trxPromise,trx);
    }).then(trx.commit)
    .catch(trx.rollback); 
  }
  return trxPromise;
}

function b(trxPromise,trx) {
  return trx("blah").select().where("fu","bar")
  .then(function(res) {
      // This is where I want to add code to occur after the trx commits
      trxPromise.then(function(){//Do stuff after trx commits})
  }
}