Skip to content

Instantly share code, notes, and snippets.

@Saul-Mirone
Created August 9, 2022 02:50
Show Gist options
  • Save Saul-Mirone/990fc2d2a5102b91df216629ad5b160a to your computer and use it in GitHub Desktop.
Save Saul-Mirone/990fc2d2a5102b91df216629ad5b160a to your computer and use it in GitHub Desktop.
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