0

I'm trying to write some parser on node. I can't understand something here. I need to stop requesting next pages if previous result was wrong. My current code is something like this:

var request = require('request');
var cheerio = require('cheerio');
var links = ['http://link1','http://link2','http://link3'];

for(l in links) {
var link = links[l];
request(link, function(err, page) {
    if(err) throw err;

    $ = cheerio.load(page.body);
    if($('a').length < 2) {
        // here i need to stop requesting next url(s) from links array        somehow!
        // (if this is the case of link1 then link2 and link3 will not  request)
    } else {
        // do something...
    }
    $.html();
});
}
yieChug1
  • 85
  • 7
  • You're requesting all links **in parallel**. I recommend taking a look at the [async](https://github.com/caolan/async) library and use `async.eachSeries`. – Prinzhorn Feb 14 '16 at 08:12
  • Possible duplicate of [Synchronous request in nodejs](http://stackoverflow.com/questions/6048504/synchronous-request-in-nodejs) – stdob-- Feb 14 '16 at 08:18

1 Answers1

0

Node.js is an asynchronous language, so you can't stop it in native javascript like break;, so you probably try using the async module like this:

var request = require('request');
var cheerio = require('cheerio');
var links = ['http://link1', 'http://link2', 'http://link3'];
var async = require('async');

async.eachSeries(links, function(link, callback) {
    request(link, function(err, page) {
        if (err) {
            callback(err);
            return;
        }
        $ = cheerio.load(page.body);
        if ($('a').length < 2) {
            // here i need to stop requesting next url(s) from links array        somehow!
            // (if this is the case of link1 then link2 and link3 will not  request)
            callback(err);
            return;
        } else {
            // do something...
        }
        $.html();
        callback();
    });
}, function(err) {
    if (err) {
        //do something id some of the links throws err
        return;
    }
    //do womething if every request was success
});

The above code will run a function for each element in the array(links), if one of the links will call callback with error, it will immidiately stop the iteration and will call the callback function with the error. Otherwise if all of the elements will call callback without any params, the callback function will be called without any params.

Please look at async docs to see other options that might help you in other ways.

Ziki
  • 1,390
  • 1
  • 13
  • 34