0

I have a Node.js package in which I have the following export:

exports.getSubredditLinks = function(url) {
  request(url, function(err, resp, body) {
    if (err) return console.error(err);
    var arr = new Array();
    $ = cheerio.load(body);
    $('a.title.may-blank').each(function(i, elem) {
      arr.push($(elem).attr('href'));
    });
    console.log(arr);
    return arr;
  });
}

However, I need to return the array created by adding a bunch of HTML elements into the array arr. It doesn't work like this, I think because it is an asynchronous function, but I don't know how to return a value from such a function.

jswny
  • 167
  • 1
  • 12

0 Answers0