I have an endpoint in my Node.js API which returns a JSON array of results provided by the Google-Search-**scraper** library.

```
app.get('/google_image_search', (req, res) => {
    var options = {
        query: 'grenouille',
        age: 'y', // last 24 hours ([hdwmy]\d? as in google URL)
        limit: 10,
        params: {} // params will be copied as-is in the search URL query string
    };

    var results = [];
    scraper.search(options, function(err, url, meta) {
        sem.take(function() { 
            if(err) throw err;

            var result = {
                title: meta.title,
                meta: meta.meta,
                description: meta.desc
            }
            results.push(result);
            sem.leave();
        });
    })

    console.log(results);

    res.json({
        results
    });
})

```

I need the console.log(results) and the res.json({ results }) to happen after the scraper.search function is done. It is currently always returning an empty array.

The function passed to the scraper.search() function is called for every result. So if there are 10 results that function runs 10 times, which is why I'm waiting until the array is full to send the response.

I have tried using semaphores and mutex locks in different places but no luck. Any advice is appreciated.

---

This was solved using a LIMIT variable to check my results array against. Outlined in the answer marked correct below.

Thanks to everyone for the input.

---