Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 = () => {
- console.log(cur.fn, cur.args);
- if (cur.fn) cur.fn(cur.args, () => {
- console.log(1);
- if (cur.next) cur.next.forward();
- })
- }
- return cur;
- }
- function foo(item) {
- console.log(item);
- }
- const c1 = chain()
- .do(foo, {a: 1, b: 2})
- c1()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement