Skip to content

Instantly share code, notes, and snippets.

@nexpr
Last active December 19, 2015 13:12
Show Gist options
  • Save nexpr/9dcf51ce9d31ce138d0c to your computer and use it in GitHub Desktop.
Save nexpr/9dcf51ce9d31ce138d0c to your computer and use it in GitHub Desktop.
var actions = [
function(next){
setTimeout(function(){
console.log(10)
next()
}, 1000)
console.log(1)
},
function(next){
setTimeout(function(){
console.log(20)
next()
}, 2000)
console.log(2)
},
function(next){
setTimeout(function(){
console.log(30)
next()
}, 3000)
console.log(3)
}
]
function*a(as){
while(1){
yield as.shift()
}
}
function next(g){
var fn = g.next().value
fn && fn(next.bind(null, g))
}
next(a(actions))
var actions = [
function(g){
setTimeout(function(){
console.log(10)
g.next()
}, 1000)
console.log(1)
},
function(g){
setTimeout(function(){
console.log(20)
g.next()
}, 2000)
console.log(2)
},
function(g){
setTimeout(function(){
console.log(30)
g.next()
}, 3000)
console.log(3)
}
]
function*a(as){
var g = yield
while(1){
yield setTimeout(function(){
var fn = as.shift()
fn && fn(g)
},0)
}
}
var g
(g = a(actions)).next()
g.next(g)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment