Created
August 9, 2022 02:50
-
-
Save Saul-Mirone/990fc2d2a5102b91df216629ad5b160a 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
export type Pipeline = (env: PipelineEnv, next: () => Promise<void>) => Promise<void>; | |
const runPipeline = (pipelines: Pipeline[]) => { | |
return (env: PipelineEnv, next?: Pipeline): Promise<void> => { | |
let index = -1; | |
const dispatch = (i: number): Promise<void> => { | |
if (i <= index) return Promise.reject(new Error('next() called multiple times')); | |
index = i; | |
let fn = pipelines[i]; | |
if (i === pipelines.length) fn = next; | |
if (!fn) return Promise.resolve(); | |
try { | |
return Promise.resolve(fn(env, () => dispatch(i + 1))); | |
} catch (err) { | |
return Promise.reject(err); | |
} | |
}; | |
return dispatch(0); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment