Created
February 14, 2024 01:44
-
-
Save forivall/4afc80145b0f845db268daa540da5d4c to your computer and use it in GitHub Desktop.
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
interface AsyncCompose { | |
<Options, R1, R2, R3>( | |
f: (options: Options) => R1, | |
g: (arg: Awaited<R1>, options: Options) => R2, | |
h: (arg: Awaited<R2>, options: Options) => R3, | |
): (options: Options) => R2; | |
<R1, R2>(f: () => R1, g: (arg: Awaited<R1>) => R2): () => R2; | |
<Options, R1, R2>( | |
f: (options: Options) => R1, | |
g: (arg: Awaited<R1>, options: Options) => R2, | |
): (options: Options) => R2; | |
} | |
const compose: AsyncCompose = | |
<Options>( | |
f: (options: Options) => unknown, | |
...gs: Array<(result: unknown, options: Options) => unknown> | |
) => | |
(options?: Options) => | |
gs.reduce(async (result, g) => g(await result, options), f(options)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment