As mentioned in the comments:
In the first case, if the condition is false the resulting stream completes immediately. In the second case, it appears you are returning no stream if the condition is false. To be comparable, the second example would need to have an else and return EMPTY. The doc for iif is here: rxjs.dev/api/index/function/iif
Using both the true and false condition, the statements would be comparable:
getUser() {
const url = `${this.userUrl}/${this.currentUserId}`;
this.todosForUser$ = this.http.get<User[]>(url).pipe(
switchMap(user => {
return iif(
() => this.todos,
this.http.get<ToDo[]>(`${this.todoUrl}?userId=${this.currentUserId}`),
this.http.get<Post[]>(`${this.postUrl}?userId=${this.currentUserId}`)
);
})
);
}
And
getUser() {
const url = `${this.userUrl}/${this.currentUserId}`;
this.todosForUser$ = this.http.get<User[]>(url).pipe(
switchMap(user => {
if (this.todos) {
return this.http.get<ToDo[]>(`${this.todoUrl}?userId=${this.currentUserId}`);
} else {
return this.http.get<Post[]>(`${this.postUrl}?userId=${this.currentUserId}`);
}
})
);
}
You can find the stackblitz here: https://stackblitz.com/edit/angular-todos-deborahk-iif
So it comes down to which you and your team think is the easiest to read.
EMPTY. The doc foriifis here: rxjs.dev/api/index/function/iif " - DeborahK