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

211
Views
Pushing to an array not working in callback

See the comments in code to see what's actually happening/not happening. Basically within the function, the array.push() method does not seem to work and I cannot wrap my head around why. Any help would be appreciated.

 var locationArray = new Array;

        request(process.env.RESOURCE_SHEET, (error, response, html) => {
            if(!error && response.statusCode == 200) {

                const $ = cheerio.load(html);
                
                $("h3").each((i, lle) => {
                    const location = $(lle).text();
                    if(location.includes("Kansas")) return;
                    if(location.includes("In Stock")) {
                        var level = location + " ✅";
                    } else {
                        var level = location + " ❌";
                    }
                    locationArray.push(level); // This doesn't push to the array
                });
            } 
        });
        locationArray.push("test") // This pushes to the array

        console.log(locationArray) // Output: "test"
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Thanks to Harsh Saini.

His response: request works asynchronously, you should try console the array inside the request callback.

Working code:

var locationArray = new Array;

request(process.env.RESOURCE_SHEET, (error, response, html) => {
    if(!error && response.statusCode == 200) {

        const $ = cheerio.load(html);
            
        $("h3").each((i, lle) => {
            const location = $(lle).text();

            if(location.includes("Kansas")) return;
            if(location.includes("In Stock")) {
                var level = location + " ✅";
            } else {
                var level = location + " ❌";
            }
            locationArray.push(level);
        });
    } 
    console.log(locationArray)
});
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!