Skip to content
Advertisement

Wait to resolve Nested loops in a Typescript Subscribe

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.

JavaScript

Advertisement

Answer

Be lazy! don’t subscribe to observable returned from UpdateCall method in CompleteParamList method, instead return it and let the caller subscribe to it. You can tap into the stream and modify the input param paramListToComplete and assuming you’re on rxjs versoin 5 or below(for newer versions you can pipe it), the code will look like this:

JavaScript

so now wherever you’re using completeParamList, you can simply subscribe to it or convert it to promise(which I do not recommend as toPromise is deprecated in newer version of rxjs)

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement