Skip to content
Advertisement

TypeScript – waiting for nested for loops to complete

I have two for loops, one nested inside the other. The first loop makes an API call. It will execute for how ever many IDs are selected by the user. I do not have the ability pass more than one ID at a time to the API. The nested loop will run for each object returned by the API and add the data to an array. My end goal is to have all data in a single array, and to pass that array to a child component using @Input(). I have researched and attempted to do this using a Promise, but something is still not quite right. I would like ngOnChanges() in the child component to only execute once all of the data has been returned – aka both for loops have completed execution. This is what I have done:

Parent Component:

JavaScript

Parent Template:

JavaScript

Child Component:

JavaScript

Advertisement

Answer

You can use Promise.All:

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