Last active
September 27, 2017 00:36
-
-
Save joedski/b7046f463e3acb67f520039ff0695236 to your computer and use it in GitHub Desktop.
comp-undo-steps 0: Example step implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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