Documentation / @andrew_l/mongo-transaction / onRollback
Function: onRollback()
onRollback(
callback
,dependencies
?):Fn
Registers a callback to be executed upon transaction rollback, with support for dependency-based updates.
This function is used within a transaction scope to perform specific actions when a transaction is rolled back. If dependencies are provided, the callback is re-registered only if the dependencies have changed. Otherwise, the callback is registered unconditionally.
Parameters
• callback: OnRollbackCallback
The function to be executed upon transaction rollback.
• 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
// Basic usage without dependencies
onRollback(() => {
console.log('Transaction rolled back!');
});
// Using dependencies
count++;
onRollback(() => {
console.log(`Rollback detected, flag is ${flag}`);
}, [count]);
// Cancel by request
const cancel = onRollback(() => {
console.log('This will run only once on rollback!');
});
if (orderReceived) {
cancel(); // Prevents onRollback from running
}