Skip to content

Instantly share code, notes, and snippets.

@mikesmullin
Last active January 2, 2016 23:59
Show Gist options
  • Save mikesmullin/8379744 to your computer and use it in GitHub Desktop.
Save mikesmullin/8379744 to your computer and use it in GitHub Desktop.
Que asynchronous flow control coffeescript micro library
Que = 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
# --
Q = Que.new()
fn1 = (a, cb) -> setTimeout (-> console.log a; cb()), 400
end = (err) -> console.log 'd, err:', err
console.log '--'
Q.then(fn1, 'hello')
Q.then(fn1, 'again')
Q.push ->
console.log 'a'
Q.next()
Q.push ->
console.log 'b'
Q.next()
Q.push ->
console.log 'c'
Q.next()
Q.then fn1, 'goodbye'
Q.finally(end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment