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

421
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
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