Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

74
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda