Skip to content

Instantly share code, notes, and snippets.

@jhunterkohler
Last active July 23, 2021 23:49
Show Gist options
  • Save jhunterkohler/3e9b30253973e88f61be469b159d972e to your computer and use it in GitHub Desktop.
Save jhunterkohler/3e9b30253973e88f61be469b159d972e to your computer and use it in GitHub Desktop.
Chain input/outputs of functions together
export function flow(...fn) {
return function (...args) {
let ret = fn[0].apply(this, args);
let index = 1;
let { length } = fn;
while (index < length) {
ret = fn.call(this, ret);
index++;
}
return ret;
};
}
export async function asyncFlow(...fn) {
return function (...args) {
let ret = await fn[0].apply(this, args);
let index = 1;
let { length } = fn;
while (index < length) {
ret = await fn.call(this, ret);
index++;
}
return ret;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment