Documentation / @andrew_l/mongo-transaction / onRollback
Function: onRollback()
onRollback(
callback,dependencies?):Fn$1
Registers a callback that runs once after the final failure, after effect cleanups. Not called between retries. A hook error is logged and does not affect the transaction result.
Parameters
callback
OnRollbackCallback
Runs after the rollback.
dependencies?
readonly any[]
When provided, the callback is replaced on retry only if they changed.
Returns
Fn$1
Cancels the callback.
Examples
ts
// Basic usage without dependencies
onRollback(() => {
console.log('Transaction rolled back!');
});ts
// Using dependencies
count++;
onRollback(() => {
console.log(`Rollback detected, flag is ${flag}`);
}, [count]);ts
// Cancel by request
const cancel = onRollback(() => {
console.log('This will run only once on rollback!');
});
if (orderReceived) {
cancel(); // Prevents onRollback from running
}