Last active
December 19, 2015 13:12
-
-
Save nexpr/9dcf51ce9d31ce138d0c to your computer and use it in GitHub Desktop.
This file contains 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
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)) |
This file contains 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
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