I have array of more then 100 items. Each item hits a url. After getting response from the specific url of the item I do insert option in the database.
If I use async await, then I have to wait till the 100 items get processd. But if I do asynchronoulsy, then I got response but as asynchronus behavior the data in db will not be visible untill all process get completed.
Example 1: using async await
var loc = async(locations){
return new Promise((resolve,reject) => {
try{
for(let item of locations){
var response = await html(location.url);
await insertDB(response);
}
}catch(e){
console.log('error : ',e);
}
resolve('All operation completed successfully');
})
}
loc(locations);
locations is array of items In this I will get response only when all the requests get completed.
Example 2: asynchronously
var loc = (locations){
return new Promise((resolve,reject) => {
locations.forEach(location => {
html(location.url,(res) => {
insertDB(response,(rows) => {
});
});
})
resolve('All operation completed successfully');
})
}
loc(locations)
Here I will immediately get the response, but the process on hitting and insertion will be processing in the background.
Considering example 1, is it possible that we can divide the looping request into child process so that make it execute fast in nodejs or any other way to do it?
