Created
June 30, 2013 02:27
-
-
Save yfuruyama/5893541 to your computer and use it in GitHub Desktop.
yield
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 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