Skip to content

Instantly share code, notes, and snippets.

@yfuruyama
Created June 30, 2013 02:27
Show Gist options
  • Save yfuruyama/5893541 to your computer and use it in GitHub Desktop.
Save yfuruyama/5893541 to your computer and use it in GitHub Desktop.
yield
function xhr(callback) {
console.log('now requesting...');
setTimeout(function () {
var response = {
status: '200'
};
callback(response);
}, 1000);
}
var generator;
var wrap = function (func) {
var newFunc = function () {
func(function (arg) {
try {
generator.send(arg);
} catch(e) {
console.log(e);
}
});
};
return newFunc;
}
var co = function (routine) {
generator = routine();
generator.next(); // start
};
co(function () {
var resp = yield wrap(xhr)();
console.log(resp);
var resp2 = yield wrap(xhr)();
console.log(resp2);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment