Skip to content

Instantly share code, notes, and snippets.

@tilast
Created February 1, 2015 19:54
Show Gist options
  • Save tilast/1c364894d3a3bed83f98 to your computer and use it in GitHub Desktop.
Save tilast/1c364894d3a3bed83f98 to your computer and use it in GitHub Desktop.
(function(){
function* test() {
let data = yield getData();
console.log(data);
if(data > 30) {
yield foo();
} else {
console.log("Fail");
}
}
(function() {
var start = function(foo){
var iter;
var proc = function(result) {
result.value.then(function(value) {
if(!result.done){
proc(iter.next(value));
}
})
}
iter = foo();
var next = iter.next();
proc(next);
}
window.control = {
start:start,
};
})();
function getData() {
return new Promise(function(resolve, reject) {
setTimeout(function(){
resolve(50);
}, 1000);
});
}
function foo() {
return new Promise(function(resolve, reject) {
setTimeout(function(){
console.log("Completed");
resolve();
}, 2000)
});
}
control.start(test);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment