I'm using Cheerio each function to parse some URLS and save all the data into MongoDB. my problem is that cheerio each function as synchronous. and I don't know when the parsing is ended to start to do something else. so How to make these functions Asynchronous ?
request(URL, function (error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
var posturl = $('a',this).attr('href');
$('article').each(function(i, element){
parse_url(posturl, i);
});
}
});
here is my Parse URL function
function parse_url(url, i) {
request(url, function (error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
var title = $('article h1').text();
var postid = $('article').attr('id').substring(5);
var image = $('article img').attr('src');
var description = $('article p strong').text();
var json = { title : title, postid : postid, image : image, decription : description};
collection.insert(json,function (err, result) {
if (err) {
console.log(err);
} else {
}
});
}
});
}