I've been trying to combine multiple async calls for a while now, and everytime I get close, I get stuck at a foreach loop I'm trying to solve.
At the moment I pass an array of categories to a function, which will return an observable containing an array of all the questions (of all the categories combined).
I was thinking of
getQuestions(categories: Category[]): Observable<Question[]> {
let q: Question[];
categories.forEach(c => {
this.cs
.getQuestionsCategory(c.id)
.pipe(map(questions => q.push(...questions)));
//return q somehow?
});
}
I would then be able to use it like this:
let result: Result;
let res: [Category[], Account, Answer[]];
return this.getResultByRouteParamId(route).pipe(
tap(resu => (result = resu)),
switchMap((result: Result) =>
this.forkJoinQuizCategoriesAccountAnswers(result)
),
tap(results => (res = results)),
switchMap(x => this.getQuestions(res[0])),
map(questions => {
// Create objects of questions, answers and category and add them to a QuestionAnswer object
// Return new UserResult containing the user and a list of QuestionAnswer objects
return new UserResult(null, null);
})
);
This is as close as I could get to someone's recommendation on my previous question. I wanted to add this to my original question, but I feel this wouldn't be the right thing to do since I'm no longer struggling with nested observables, but rather with looping over them.
EDIT
Tried something else, but I doubt this is the right way to do it
getQuestions(categories: Category[]): Observable<Question[]> {
let ques = categories.map(c =>
this.cs.getQuestionsCategory(this.auth.token, c.id)
);
return merge(...ques);
}