Skip to content

Instantly share code, notes, and snippets.

@joedski
Last active September 27, 2017 00:36
Show Gist options
  • Save joedski/b7046f463e3acb67f520039ff0695236 to your computer and use it in GitHub Desktop.
Save joedski/b7046f463e3acb67f520039ff0695236 to your computer and use it in GitHub Desktop.
comp-undo-steps 0: Example step implementation
function stepA(options) {
return (next) => async (pctx) => {
// Do stuff, allowing any errors to reject this promise,
// passing them back to previous steps.
const result = await doSomething(pctx.foo);
const nextPctx = { ...pctx, bar: result };
try {
// NOTE: Technically, you could further process the result
// before returning it.
return await next(nextPctx);
}
catch (error) {
// Undo stuff. If an error occurs here, it will still result
// in previous steps trying to undo themselves.
// If an undo step errors, combine the errors in some manner
// so we don't lose debug info.
try {
await undoSomething(pctx.foo);
}
catch (undoError) {
throw combineErrors(error, undoError);
}
// If undo was a success, propagate the error back.
throw error;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment