Skip to content

Instantly share code, notes, and snippets.

@chenzhihao
Last active August 29, 2015 14:06
Show Gist options
  • Save chenzhihao/927372e1f95272fe75d4 to your computer and use it in GitHub Desktop.
Save chenzhihao/927372e1f95272fe75d4 to your computer and use it in GitHub Desktop.
a simple co
function tunkfiedSettimeOut(delay,index) {
return function(cb) {
console.log('index: ',index);
setTimeout(function(){
cb(undefined,delay);
},delay);
}
}
tunkfiedSettimeOut(3000,1)(function(err,data){console.log("delay is", data)});
tunkfiedSettimeOut(0,2)(function(err,data){console.log("delay is", data)});
function *test() {
var a = yield tunkfiedSettimeOut(3000,1);
var b = yield tunkfiedSettimeOut(0,2);
console.log("delay is", a);
console.log("delay is", b);
}
function co(producer){
return function() {
var TG = producer();
var result = TG.next();
(function next(result){
if (result.done){
return;
}
var tunkfied = result.value;
tunkfied(function (err,data){
if (err) {
TG.throw(err);
}
var result = TG.next(data);
next(result);
})
})(result);
}
}
var TG = test();
var result = TG.next();
(function next(result){
if (result.done){
return;
}
debugger;
var tunkfied = result.value;
tunkfied(function (err,data){
var result = TG.next(data);
next(result);
})
})(result);
var a = TG.next();
function *ah() {
var a = yield setTimeout(function(){
console.log("wow");
},5000);
setTimeout(function(){
console.log("yeah");
},0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment