Reputation: 1347
I trying to run huge array of urls with nodejs async.eachSeries. I want to show success msg after process done. But here in my code the success msg just come out before all the process complete. Please take a look at my code:
var request = require("request"),
cheerio = require("cheerio"),
mysql = require('mysql'),
async = require('async');
carlistrequest=request.defaults({
headers: {
'User-Agent':'request'
}
});
var callUrls =[
'http://www.carlist.my/used-cars/2625750/2006-mercedes-benz-s350-amg-w221-full-loan.html',
'http://www.carlist.my/used-cars/2625770/2010-toyota-estima-2-4-aeras-unregister-full-loan.html',
'http://www.carlist.my/used-cars/2625739/2012-honda-city-1-5-e-facelift-full-loan.html',
'http://www.carlist.my/used-cars/2536923/2002-bmw-318i-e46-2-0-cash-only-012-634-6341.html',
'http://www.carlist.my/used-cars/2625567/-loan-up-to-rm43k-2009-toyota-vios-1-5-j-spec-a-trd-bodykit-2015-final-clear-stock-hurry-up-.html'
];
async.eachSeries(callUrls, function(uri, next){
carlistrequest(uri, function(err, response, body){
var $ = cheerio.load(body);
var data={
name: $('#main-sidebar').find('.sub').text()
}
console.log(data);
})
next();
},function(callback){
console.log('All Url Done');
})
How to run url one by one and after all urls finish running, just show success msg. Thanks:)
Upvotes: 0
Views: 228
Reputation: 13672
Move next()
inside the callback of your request.
async.eachSeries(callUrls, function (uri, next) {
carlistrequest(uri, function (err, response, body) {
var $ = cheerio.load(body);
var data = {
name: $('#main-sidebar').find('.sub').text()
}
console.log(data);
next(); // <<<<<<<<<<<<
})
}, function (callback) {
console.log('All Url Done');
});
next()
tells async when the function(uri, next)
is done with one url, if you call it outside your request's callback, you don't wait the request to be finished.
Another side effect, in your code, the iteration is not done in series like it should when you call eachSeries()
, multiple requests are running at the same time. If you have hundreds of urls to request, you'd be banned from the server you're querying.
Upvotes: 1
Reputation: 13972
It looks like your next()
call needs to be inside the carlistrequest
callback, not beside it
I'm guessing carlistrequest
is asynchronous (given it takes a callback)... so you fire off the async request then immediately tell async.eachSeries
that this iteration is complete (but it's not - it still has work to do.)
Upvotes: 0