const co = require('co');
const func1 = function(){
setTimeout(()=>{
console.log('func1');
},2000);
}
const func2 = function(){
setTimeout(()=>{
console.log('func2');
},2000);
}
const func3 = function(){
console.log('func3');
}
const gen = co.wrap(function*(){
yield func1;
yield func2;
return yield func3;
});
gen()
.then(function(){console.log('end')});
The expected result is func1 func2 func3 end
but it doesn't show what I intended.
It is showing func1
How can I fix the code to output the expected result
Two issues:
There's no way for your functions to return control. If you want to use thunks (which the documentation advises against), you need to actually take and invoke a callback:
However, it's better to use promises:
If you use promises, you need to invoke the function when you yield it:
You can only
yielda function without calling it if it's a thunk.