Created
July 7, 2019 18:01
-
-
Save KamMif/6ac59e3d2068e40e6e271a01103e962b to your computer and use it in GitHub Desktop.
async function chain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment