0

do i need to keep each link in a array within "request({..})", and then display it or work on the outside of "request({..})", this would be my code but does not work, any idea?

var request = require("request");
var cheerio = require("cheerio");
var arrayLinks = [];
request({
    uri: "http://www.some-url.com",
}, function(error, response, body) {
    var $ = cheerio.load(body);
    $("a").each(function() {
    var link = $(this);
    arrayLinks.push(link.attr("href"));
    });
});
arrayLinks.forEach(function(link){console.log(link)});
0

1 Answer 1

2

For example:

var request = require("request");
var cheerio = require("cheerio");
var arrayLinks = [];
request({
    uri: "http://www.some-url.com",
}, function(error, response, body) {
    // Some logic.
    linkTheArray()
});

function linkTheArray() {
     arrayLinks.forEach(function(link){console.log(link)});
}

Now you can run it after the request is done. There is one other way, but it is pretty ugly. You can run a timeout function, until you get some data in the array

Sign up to request clarification or add additional context in comments.

1 Comment

Now I understood it, thank you for your explanation was more clear with example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.