Created
August 30, 2024 20:16
-
-
Save jehna/7613eeefd06df8e86bb7b4de3201e277 to your computer and use it in GitHub Desktop.
saga.ts
This file contains 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
export type SideEffect<Result> = { | |
commit: () => Promise<Result>; | |
rollback: () => Promise<any>; | |
}; | |
export async function saga<T>( | |
callback: ( | |
run: <Result>(sideEffect: SideEffect<Result>) => Promise<Result> | |
) => Promise<T> | |
): Promise<T> { | |
const rollbacks: SideEffect<any>["rollback"][] = []; | |
async function run<Result>(sideEffect: SideEffect<Result>): Promise<Result> { | |
try { | |
const result = await sideEffect.commit(); | |
rollbacks.unshift(sideEffect.rollback); | |
return result; | |
} catch (e) { | |
for (const rollback of rollbacks) { | |
await rollback(); | |
} | |
throw e; | |
} | |
} | |
return await callback(run); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment