Reputation: 63
i'm new with Node.Js. I want to make a function call in another function and when everything is finished, print a file.
But the functions are asynchronous so when the first ends node print the file without waiting the second one..
I tried the waterfall method of Async library but is not the waterfall that i need.. Async is a big library with a lots of different patterns, maybe one of these are the solution
This is the code:
request(url, function (error, response, html) {
var json_list = [];
if (!error) {
var $ = cheerio.load(html);
$('.fixed-recipe-card').each(function () {
var json = {title: "", description: "", rating: "", ingredients: [], url: ""};
json.title = $(this).children().last().children().first().text().trim();
json.description = $(this).children().last().children().first().next().children().last().text().trim();
json.rating = $(this).children().last().children().first().next().children().first().children().first().attr('data-ratingstars');
json.url = $(this).children().last().children().first().next().attr('href');
request(json.url, function (error, response, html) {
var $ = cheerio.load(html);
$('.checkList__line').filter(function () {
var ingredient = $(this).children().first().children().last().text().trim();
if (ingredient !== "Add all ingredients to list") {
console.log(ingredient);
json.ingredients.push(ingredient);
}
});
});
json_list.push(json);
});
}
fs.writeFile('output.json', JSON.stringify(json_list, null, 4), function (err) {
console.log('success');
})
res.send(JSON.stringify(json_list));
});
Upvotes: 2
Views: 100
Reputation: 1817
You could use Promises:
let promises = [];
$('.fixed-recipe-card').each(function () {
var json = {title: "", description: "", rating: "", ingredients: [], url: ""};
json.title = $(this).children().last().children().first().text().trim();
json.description = $(this).children().last().children().first().next().children().last().text().trim();
json.rating = $(this).children().last().children().first().next().children().first().children().first().attr('data-ratingstars');
json.url = $(this).children().last().children().first().next().attr('href');
let p = new Promise(function(resolve, reject) {
request(json.url, function (error, response, html) {
if(error) { reject(error); } // reject promise on error
var $ = cheerio.load(html);
$('.checkList__line').filter(function () {
var ingredient = $(this).children().first().children().last().text().trim();
if (ingredient !== "Add all ingredients to list") {
console.log(ingredient);
json.ingredients.push(ingredient);
}
});
resolve(response); // promise success
});
});
promises.push(p);
});
Promise.all(promises).then(function(values) {
console.log(values);
fs.writeFile('output.json', JSON.stringify(json_list, null, 4), function (err) {
console.log('success');
})
...
});
...
With Promise.all you can execute something when all async calls (built as promises) have been resolved
Check this link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
Hope this helps
Upvotes: 1