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

415
Vistas
Access variable inside javascript function (generator)

In Javascript suppose I have a generator which i cannot modify the source code of. I iterate through a couple of times and now want to look inside at the variables of the generator. How can I do this without changing the code of the generator itself? More concretely -

async function* myGen(){
    while (true){
        let a = something
        yield something_else
    } 
}

let gen = myGen()

for await (const data of gen){
    if(data === special_value){
        let my_a = get_value_of_a(gen) // this is the function i want
    }
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If the generator doesn't yield the value of a (or something that gives you access to the value of a), you cannot access it. a is a local variable within the generator function. It's not accessible from outside that function (unless you do something to make it accessible, but you said you can't modify the generator function).

Putting it another way: The value of a is private information held inside the generator object the function returns. You can't access that private information if the generator object doesn't provide a means of doing so, which the ones created by generator functions don't by default.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Global variables can be accessed from anywhere in a class or namespace. ... If a variable is defined in any of these scopes, it will be visible to the current scope and the ones that are nested within it. In simple terms: A variable declared within a loop will not be visible outside the loop.

    var global = "Global Variable"; //Define global variable outside of function.
​    function setGlobal(){
    global = "Hello World!";
    };
    setGlobal();
    console. log(global); //This will print out "Hello World"
about 4 years ago · Juan Pablo Isaza Denunciar
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