I am trying to run a series of functions in sequence by storing them in an array (specifically for an Angular APP_INITIALIZER function).
Here is the array in question:
const obsArray = [
myService1.init(),
myService2.init(),
...
myServiceN.init()
]
Each of these init()
functions returns an Observable<void>
. Here's an example:
init() : Observable<void> {
return this.http.get(this.apiUrl)
.pipe(
// do something with the response
switchMap(() => EMPTY)
);
}
The switchMap
statement ensures that the return type is Observable
.
I've attempted this approach:
const init$ : Observable<void> = forkJoin(obsArray);
However, it seems to execute the function but not the HTTP call within. Since this is within an Angular factory function assigned to the APP_INITIALIZER
token, there is no subscribe()
call.
I've also tried using concatMap()
without success.
Is there an rxjs function that can run each of these functions sequentially, waiting for the previous one to complete?
I have posted a related question on Stack Overflow.