Consider I have the following code:
function a() { console.log("A"); }
function b() { console.log("B"); }
function* test(code) {
eval(code);
yield abc;
}
var code = "var abc = 2; a(); yield 1; b();"
var testObj = test(code);
var first = testObj.next(); // expect 1
var second = testObj.next(); // expect 2
It gives the following error:
Uncaught SyntaxError: Unexpected number
Wrapping the code in another generation function is not possible as the variable "abc" will be gone. Is there any solution on that?
Update on 8 Aug: The variable scope needs to be preserved, because there will be multiple codes to be executed (each probably triggered by events).
function* test() {
while (true) {
eval(code);
}
yield abc;
}
var testObj = test();
// First Call
var code = "var a = 2;"
testObj.next();
// Second Call
code = "a += 1;"
testObj.next();
// Third Call
code = "console.log(a);"
testObj.next(); //Expect Output "3"