Last active
January 2, 2016 23:59
-
-
Save mikesmullin/8379744 to your computer and use it in GitHub Desktop.
Que asynchronous flow control coffeescript micro library
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
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