Skip to content

Instantly share code, notes, and snippets.

@dmitrymatveev
Created March 28, 2018 21:30
Show Gist options
  • Save dmitrymatveev/c9724ebcb076fa8c918ea5d66b801b66 to your computer and use it in GitHub Desktop.
Save dmitrymatveev/c9724ebcb076fa8c918ea5d66b801b66 to your computer and use it in GitHub Desktop.
const HEAD = 0;
const splitCallback = list => [
list.slice(0, list.length - 1),
list[list.length - 1]
];
function next(args, done, ...functions) {
const fnc = functions[HEAD];
if (!fnc) done();
else fnc(...args, err => {
if (err) done(err);
else next(args, done, ...functions.slice(1))
});
}
export const waterfall = (...functions) => (...args) => next(...splitCallback(args), ...functions);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment