Skip to content

Instantly share code, notes, and snippets.

@lwdgit
Created April 24, 2016 08:54
Show Gist options
  • Save lwdgit/74b3e7417e6a853ce9c5466a51841cad to your computer and use it in GitHub Desktop.
Save lwdgit/74b3e7417e6a853ce9c5466a51841cad to your computer and use it in GitHub Desktop.
function* wrap() {
yield sleep(1000);
yield sleep(2000);
}
function sleep(time) {
return function(done) {
setTimeout(function() {
console.log('sleep ' + time + 'ms');
done(null, time);
}, time);
}
}
function co(generator, err, res) {
var ret = generator.next(res);
if (ret.done) return;
ret.value(function(err, res) {
co(generator, err, res);
});
}
co(wrap());
function* wrap2() {
yield sleep2.call(this, 4000);
yield sleep2.call(this, 1000);
}
function sleep2(time) {
setTimeout(function() {
console.log('sleep ' + time + 'ms');
this.next();
}.bind(this), time)
}
function co2(generator) {
new generator().next();
}
co2(wrap2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment