Last active
August 29, 2015 14:02
-
-
Save Gerhut/5133d7dfcfa381474434 to your computer and use it in GitHub Desktop.
简单的函数式回调链构造器
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
| #!/usr/bin/env coffee | |
| module.exports = SimpleAsync = (funcs...) -> | |
| callback = (args...) -> | |
| funcs.shift()? args..., callback | |
| return | |
| inner = (argsOrFuncs...) -> | |
| if typeof argsOrFuncs[0] is 'function' | |
| funcs.push argsOrFuncs... | |
| inner | |
| else | |
| callback argsOrFuncs... | |
| return | |
| if require.main is module | |
| func1 = (id, leave, callback) -> | |
| console.log "id #{id} in func1 leave #{leave}" | |
| setTimeout callback, 100, id, 'func1' | |
| func2 = (id, leave, callback) -> | |
| console.log "id #{id} in func2 leave #{leave}" | |
| setTimeout callback, 200, id, 'func2' | |
| func3 = (id, leave, callback) -> | |
| console.log "id #{id} in func3 leave #{leave}" | |
| setTimeout callback, 300, id, 'func3' | |
| SimpleAsync(func1)(func2, func3)(0, 'none') | |
| SimpleAsync(func3, func2)(func2, func1)(1, 'none') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment