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

416
Views
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 answers
Answer question

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 Report

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 Report
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!