Created
April 20, 2014 21:24
-
-
Save rikukissa/11125662 to your computer and use it in GitHub Desktop.
ES6 Generator testing
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
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