Goal: Ensure all nested loops complete processing before returning final value.
Problem: Final value returned prematurely, before completion of loop processing.
In the code snippet below, I am sending paramListToComplete
to a data service for creating a record and fetching the new record in the same call. This allows me to update missing values in paramListToComplete
and return the list with updated values from the service. However, the function currently returns the list before finishing all the loops, leaving my paramListToComplete
incomplete.
Is there a way to ensure that all loops are processed before returning or perhaps convert the nested loops into promises and await their resolution? Any assistance on this matter would be greatly 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)));
}