Skip to content

Instantly share code, notes, and snippets.

@sbimikesmullin
Last active March 3, 2018 23:42
Show Gist options
  • Save sbimikesmullin/88b6548c76fa3232e7e8 to your computer and use it in GitHub Desktop.
Save sbimikesmullin/88b6548c76fa3232e7e8 to your computer and use it in GitHub Desktop.
Q: oversimplified asynchronous flow control
r = (
((next)->
execute "#{if o?.sudo then 'sudo ' else ''}mkdir #{if o?.recursive then '-p ' else ''}#{if o?.chmod then '-m'+o.chmod+' ' else ''}#{name}", next
return
)(->((next)->
#return next() unless o?.user or o?.group
execute "#{if o?.sudo then 'sudo ' else ''}chown #{o?.user}:#{o?.group} #{name}", next
return
)(cb))
)
# will execute top to bottom and then call cb
# oversimplified asynchronous flow control
module.exports = new: ->
Q = []
Q.then = (fn, args...) -> Q.push(-> args.push Q.next; fn.apply null, args); @
Q.next = (err) -> Q.splice 0, Q.length-1 if err; Q.shift()?.apply null, arguments
Q.finally = (fn, args...) -> Q.push(-> fn.apply null, args); Q.next()
Q
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment