I'm in the process of developing a service method that fetches data from an API and returns it as a promise. The challenge here is that I need to ensure that the parent object, along with all its child objects, are fully retrieved before resolving the outer promise. The code snippet below demonstrates what I have so far, but I am struggling to make the promise wait for all child objects to be resolved before completion.
private obtainRegistrationData(url: string): Promise<MyType> {
return new Promise((resolve) => {
this.dataService.fetchMyType(url)
.then((myType: MyType) => {
_.forEach(myType.childReferences, (childUrl) => {
let childPromise = this.dataService.getChild(childUrl).then((child) => {
myType.children.push(child);
};
};
return x;
});
});