Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

73
Views
Using yield inside eval?

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"
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!