I want a function to pipe together a series of promise returning functions. However, I also want to specify an undo function for each piped function. If any function returns a rejected promise, the undo function corresponding to that step should be called.
I'm free to let my undo function return a resolved promise, which indicates a recovery and continues down the pipe, or a rejected promise, which results in breaking the promise chain and skipping to the catch
statement (if any).
Based on this SO answer, this is the form of the solution if you know how many steps you have:
steps[1]
.then(
(...args) => steps[2](...args).then(null, undos[2]),