The goal is to loop through an Array of IDs and for each ID make an API Call and dispatch an Action with the result.
My current idea:
someEffect$ = createEffect(() => this.actions$.pipe(
ofType(someAction),
withLatestFrom(this.store.select(selectIds)),
switchMap(([_, ids]) => {
return forkJoin(ids.map(id =>
this.service.getData(id))).pipe(
map(result => {
return actionSuccess({result});
}),
catchError((error: HttpErrorResponse) => {
return of(actionError({error}));
})
)
})
));
This kinda works, but has 2 unwanted behaviours:
- actionSuccess gets only dispatched once and with the result of all api calls as an array. I want one dispatch for each API Call.
Replacing forkJoin with merge triggers an action for each ID, but then the API Calls are no longer happening, so there has to be more to it.
- forkJoin waits for all API Calls to finish and only then continues. I want to dispatch actionSuccess immediately after every time getData() finishes.