Skip to content

Instantly share code, notes, and snippets.

@tj
Created February 5, 2010 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tj/295514 to your computer and use it in GitHub Desktop.
Save tj/295514 to your computer and use it in GitHub Desktop.
var sys = require('sys')
function Queue() {
this.arr = []
}
Queue.prototype.push = function(fn) {
this.arr.push(fn)
}
Queue.prototype.run = function() {
var self = this
if (this.arr.length)
this.arr.shift()(function(){
self.run()
})
}
queue = new Queue
queue.push(function(next){
setTimeout(function(){
sys.puts('1')
next()
}, 1000)
})
queue.push(function(next){
setTimeout(function(){
sys.puts('2')
next()
}, 200)
})
queue.push(function(next){
setTimeout(function(){
sys.puts('3')
next()
}, 100)
})
queue.push(function(next){
setTimeout(function(){
sys.puts('4')
next()
}, 300)
})
queue.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment