I am facing an issue where I need to fetch a new result based on the old result. When a specific parameter in my primary call is X, it should trigger another function. However, the problem I'm encountering is that the scope of the program continues running, leading to the loss of the new parameter.
Here's how the code appears:
export class DashboardGuard implements CanActivate {
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
this.logger.info(DashboardGuard::canActivate());
return Observable.create((subscribe: Subscriber<boolean>): void => {
Observable.forkJoin(
this.mapSettingsService.fetch(),
this.userSettingsService.fetch(),
this.mapFenceService.fetch(),
this.markersMotesService.fetch(),
this.markersStaticsService.fetch()
).subscribe((result: [MapSettings, UserSettings, MapFenceGroup[], MarkersMotes, MarkersStatics]): void => {
// Retrieve loaded motes and statics
const mapSettings: MapSettings = result[0];
const userSettings: UserSettings = result[1];
const mapFenceGroups: MapFenceGroup[] = result[2];
let markersMotes: MarkersMotes = result[3];
const markerStatics: MarkersStatics = result[4];
for (let i in markersMotes.items) {
if (markersMotes.items[i].longitude === 0 || markersMotes.items[i].latitude === 0) {
// In this scenario, I need to trigger another fetch using data from previous fetch calls, but the program scope needs to wait until those fetch calls complete.
// Example of fetch:
Observable.forkJoin(
this.markersMotesService.newfetch(markersMotes.items[i].macAddress),
).subscribe((result: [MarkersNolocations]): void => {
const newResult: MarkersNolocations = result[0];
if (newResult.missedCounter > 0) {
markersMotes.items[i].latitude = 10;
markersMotes.items[i].longitude = 10;
}
});
}
}
})
While forkjoin works great for retrieving all calls at once, what about nested calls within it?
Any assistance on this matter would be highly appreciated!
Best regards,
Bram