janus-transaction
Janus-Transaction lets you utilizes Caolan's async
to perform asynchronous transactional rollbacks.
This operates like a a normal async.waterfall
process until an error occurs and then each step of the waterfall will be reversed via custom rollback methods.
Installation
npm install janus-transaction
API
add(task, rollback)
task is the function that is called during the transaction processing. The callback to the next step of the transaction is always at the end of the task's parameters. Passing arguments into this callback will populate the next task's parameters. If the first parameter of this callback is populated with a non-falsy value, then the rollback process will begin.
rollback is function that is called when a task fails. The parameters of this function are populated with the arguments that were passed into the subsequent task. If the first parameter of this callback is populated with a non-falsy value, then the rollback process will prematurely finish.
This function returns the transaction object for a fluent interface.
exec(tasksFinished, rollbacksFinished)
tasksFinished is called when the tasks chains are successfully executed. The parameter will be populated with the second argument that was passed into the last task's next step callback.
rollbacksFinished is called when the rollback chains are successfully executed. The first parameter contains the original error that triggered the rollbacks. The second parameter contains any errors that happened during the rollback chain. The third parameter contains the second argument that was passed into the last rollback' next step callback.
Examples
High-Level Comprehensive Case
;
Common Use Case
var value = 0; ;
Rollback Use Case
var value = 0; ;