Skip to content

Instantly share code, notes, and snippets.

@Gerhut
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save Gerhut/5133d7dfcfa381474434 to your computer and use it in GitHub Desktop.

Select an option

Save Gerhut/5133d7dfcfa381474434 to your computer and use it in GitHub Desktop.
简单的函数式回调链构造器
#!/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