Skip to content

Documentation / @andrew_l/mongo-transaction / onCommitted

Function: onCommitted()

onCommitted(callback, dependencies?): Fn

Registers a callback to be executed upon transaction commitment, with support for dependency-based updates.

This function is used within a transaction scope to perform specific actions when a transaction is committed. If dependencies are provided, the callback is re-registered only if the dependencies have changed. Otherwise, the callback is registered unconditionally.

Parameters

callback: OnCommittedCallback

The function to be executed upon transaction commitment.

dependencies?: readonly any[]

An optional array of dependencies to determine if the callback should be re-registered. If the dependencies differ from the previously registered ones, the callback is updated.

Returns

Fn

A cleanup function to cancel event listener.

Examples

ts
// Basic usage without dependencies
onCommitted(() => {
  console.log('Transaction committed!');
});
ts
// Using dependencies
count++;
onCommitted(() => {
  console.log(`Commit #${count}`);
}, [count]);
ts
//  Cancel by request
const cancel = onCommitted(() => {
  console.log('This will run only once!');
});

if (orderReceived) {
  cancel(); // Prevents onCommitted from running
}