Skip to content

Instantly share code, notes, and snippets.

@rikukissa
Created April 20, 2014 21:24
Show Gist options
  • Save rikukissa/11125662 to your computer and use it in GitHub Desktop.
Save rikukissa/11125662 to your computer and use it in GitHub Desktop.
ES6 Generator testing
function getData(callback) {
setTimeout(function() {
callback(null, Date.now());
}, 1000);
}
function asyncCallback(generator) {
return function() {
var resolve = function(gen, data) {
var next = gen.next(data);
var fn = next.value;
if (next.done) return;
fn(function(err, data) {
resolve(gen, data);
});
};
resolve(generator());
};
}
var fn = asyncCallback(function* () {
var time = yield getData;
console.log(time);
var time2 = yield getData;
console.log(time2);
});
fn();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment