Skip to content

Documentation / @andrew_l/mongo-transaction / onMongoSessionCommitted

Function: onMongoSessionCommitted() ​

Call Signature ​

onMongoSessionCommitted<T>(fn): OnMongoSessionCommittedResult<T>

Executes the provided function upon transaction commit.

Returns T if the transaction is committed and the function completes successfully.

Returns false if the transaction ends without committing.

Rejects if the function throws an error.

Type Parameters ​

T ​

T

Parameters ​

fn ​

() => Awaitable<T>

Returns ​

OnMongoSessionCommittedResult<T>

Example ​

ts
const { promise } = onTransactionCommitted(async () => {
  console.info('Transaction committed successfully!');
  return Math.random(); // Random value generated after commit
});

promise.then(result => {
  if (result !== false) {
    console.info('Handler result:', result); // e.g., Handler result: 0.07576196837476501
  }
});

Call Signature ​

onMongoSessionCommitted<T>(session, fn): OnMongoSessionCommittedResult<T>

Executes the provided function upon transaction commit.

Returns T if the transaction is committed and the function completes successfully.

Returns false if the transaction ends without committing.

Rejects if the function throws an error.

Type Parameters ​

T ​

T

Parameters ​

session ​

ClientSession

fn ​

() => Awaitable<T>

Returns ​

OnMongoSessionCommittedResult<T>

Example ​

ts
const { promise } = onTransactionCommitted(async () => {
  console.info('Transaction committed successfully!');
  return Math.random(); // Random value generated after commit
});

promise.then(result => {
  if (result !== false) {
    console.info('Handler result:', result); // e.g., Handler result: 0.07576196837476501
  }
});