I had a problem with Angular 2 HTTP service call within for loop. The Loop run fast as compare to the service call return the result too slow.
this.projectService.addProjectServices(pid,rid,vname,field_name,ser_name).subscribe(
pdata => {
for(var k=0;k<pdata['revision_info'][0].office_info[0].vertical_info[index].service_selection_info.length;k++) // Find service index
{
if(ser_name==pdata['revision_info'][0].office_info[0].vertical_info[index].service_selection_info[k].service_name)
{
s_index=k;
}
}
for(var k=0;k<pdata['revision_info'][0].building_info.length;k++)
{
var bid=pdata['revision_info'][0].building_info[k]._id;
// Checked Building only inserted
if($("#"+bid+sid).prop("checked") == true)
{
var bid=$("#"+bid+sid).attr('build-id');
var build_name=$("#"+bid+sid).attr('build-name');
var service_name=$("#"+bid+sid).attr('service-name');
alert(build_name);
// Update
for(var n=0;n<pdata['revision_info'][0].office_info.length;n++) // inserted at single office or both office
{
var field_name='revision_info.$.office_info.'+n+'.vertical_info.'+index+'.service_selection_info.'+s_index+'.serviceselect_building_info';
projectServiceRef.projectService.updateProjectServices(pid,rid,vname,field_name,service_name,bid,build_name).subscribe(
pdata => {
alert("updated");
});
}
}
}});
In the above coding, first I made an HTTP service call to add a particular service document, then I found the index of the currently inserted service using for loop. The next for loop is to insert the subdocuments of the above service document. Here the second for loop run faster, but the subdocuments updated operation is too delay or some time update operation is not done. Anyone can help to solve my issue.