0

how to loop curl/request when each request is done? like php/blocking. its currently using loop foreach async-foreach and request.

my code:

forEach(array_lines, function(page_url, index, arr) {
        request(page_url, function (err, resp, body) {
        if (err) {
            console.log("Error!: " + err + " using ("+j+")" + page_url);
            throw err;
        }
        var $ = cheerio.load(body,{ decodeEntities: false,xmlMode: true });

        console.log('page_url: ',page_url);

        build_json.items[j] = {};

        var id = $("#xx").val();
        console.log('id: ',id);
        build_json.items[j].id = id;

        build_json.items[j].source_url = page_url;

        var title = $("h1.title").text();
        console.log('title: ',title);
        build_json.items[j].title = title;
    });
});
Boy
  • 582
  • 1
  • 5
  • 22
  • you should look into Promises –  Jul 19 '18 at 12:08
  • Possible duplicate of [Nodejs Synchronous For each loop](https://stackoverflow.com/questions/23693803/nodejs-synchronous-for-each-loop) – nijm Jul 19 '18 at 12:15

0 Answers0