Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

83
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda