Objective: I want to wait for all the nested loops to process before I can return final value.
Issue: Final value is returned before all the loops are processed.
In the below code I am sending paramListToComplete
to a data service to create a record and get that new record in the same call, so that I can set missing values in paramListToComplete
and return paramListToComplete
with updated values from service. The problem is the function returns the list before processing all the loops, so my paramListToComplete
remains incomplete.
Does anybody know how to wait for all the loops to process or maybe convert all these nested loops to a promise and wait for all of them to resolve? Any help is appreciated.
CompleteParamList(paramListToComplete): any {
this.dataService.UpdateCall(paramListToComplete)
.subscribe(
data => {
if (data) {
for (var server of paramListToComplete) {
for (var updatedData of data) {
if (paramListToComplete.length === 1 && !server.Name) {
server.Name = updatedData.Name;
}
if (!server.serverCode && server.Name === updatedData.Name) {
server.serverCode = updatedData.serverCode;
for (var serverGroup of server.serverGroups) {
serverGroup.serverCode = updatedData.serverCode;
}
for (var updatedserverGroup of server.UpdatedserverGroups) {
updatedserverGroup.serverCode = updatedData.serverCode;
}
}
}
}
}
}
);
return paramListToComplete;
}
UpdateCall(bdy: Array<testParams>) {
let url = 'endpoint/path';
let body = bdy;
let options;
return this.service.getToken()
.map(Response =>
options = new RequestOptions({
headers: this.httpHelperService.buildHttpHeader(Response.json())
}))
.mergeMap(() => this.http.put(url, body, options)
.map(this.extractData)
.catch((this.handleError)));
}