I would like to place js generator as an object property and operate on it from within other object properties. The code execution would be triggered via events. Below is short version of the code idea:
var obj = {
property1: {
generator: function* (){
console.log("gen start");
yield;
console.log("gen part 1");
},
gener: function() {
return obj.property1.generator();
},
addMonthsGrid: function() {
console.log("genNext 1");
obj.property1.gener().next();
}
},
property2: {
exec2: function() {
console.log("intermediate stuff...");
console.log("genNext 2");
obj.property1.gener().next();
}
}
};
/* first event that should run first part of generator: */
obj.property1.addMonthsGrid();
/* second event hat should run second part of generator: */
obj.property2.exec2(); //here it starts from the beggining
The problem is that the second call starts generator from the beggining (not continue it). I suppose that the problem might be in obj.property1.gener , which returns the generator, but I don't know how to make it works. I would be grateful for your help!.
Yes, gener
does create a new generator every time it is called.
const obj = {
generatorFunction: function* (){
console.log("gen start");
yield;
console.log("gen part 1");
},
generate: function() {
this.generator = this.generatorFunction();
return this.generator;
},
addMonthsGrid: function() {
console.log("genNext 1");
this.generator.next();
}
};
With this you can do
obj.generate();
obj.addMonthsGrid();
obj.generator.next();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.