Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. function chain(prev = null) {
  2. const cur = () => {
  3. if (cur.prev) {
  4. cur.prev.next = cur;
  5. cur.prev();
  6. } else {
  7. cur.forward();
  8. }
  9. }
  10. cur.prev = prev;
  11. cur.fn = null;
  12. cur.args = null;
  13.  
  14. cur.do = (fn, ...args) => {
  15. cur.fn = fn;
  16. cur.args = args;
  17. return chain(cur);
  18. };
  19.  
  20. cur.forward = () => {
  21. console.log(cur.fn, cur.args);
  22. if (cur.fn) cur.fn(cur.args, () => {
  23. console.log(1);
  24. if (cur.next) cur.next.forward();
  25. })
  26. }
  27. return cur;
  28. }
  29.  
  30. function foo(item) {
  31. console.log(item);
  32. }
  33.  
  34. const c1 = chain()
  35. .do(foo, {a: 1, b: 2})
  36.  
  37. c1()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement