Skip to content

Instantly share code, notes, and snippets.

@DIY0R
Created September 12, 2024 15:49
Show Gist options
  • Save DIY0R/c52737b9d1870fc232e539394a892ace to your computer and use it in GitHub Desktop.
Save DIY0R/c52737b9d1870fc232e539394a892ace to your computer and use it in GitHub Desktop.
function chain(prev = null) {
const cur = () => {
if (cur.prev) {
cur.prev.next = cur;
cur.prev();
} else {
cur.forward();
}
};
cur.prev = prev;
cur.fn = null;
cur.args = null;
cur.do = (fn, ...args) => {
cur.fn = fn;
cur.args = args;
return chain(cur);
};
cur.forward = () => {
if (cur.fn) cur.fn(cur.args, () => {
if (cur.next) cur.next.forward();
});
};
return cur;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment