I need to run generator async (I need to have result in console 1,2,3,4,5 cause now I have 4,1,2,3,5) any one can help me? I need run task and wait when previous task is finished before it run next task. I need to use (if possible: only) generators (or generator + promise?)
Here my code
/*jshint esnext: true */
function show(msg) {
var _msg = msg;
setTimeout(function() { console.log(_msg);}, 2000);
}
function show2(msg) {
console.log(msg);
}
var stack = [];
// add some function to stack
stack.push(function() { show(1); });
stack.push(function() { show(2); });
stack.push(function() { show(3); });
stack.push(function() { show2(4); });
stack.push(function() { show(5); });
function* generator1() {
for(var key of stack) {
yield key();
}
}
var gen = generator1();
gen.next();
gen.next();
gen.next();
gen.next();
gen.next();