If you find yourself in this situation, it's likely that you need to use either a combineLatest
or a forkJoin
. Without seeing the code for the Function getEditableText()
, we can't determine for certain.
Which operator should be used?
Use forkJoin
when waiting for both input observables to complete.
Opt for combineLatest
if you want emissions as soon as both observables emit once (not necessarily complete).
After reviewing your question, I strongly recommend revisiting RxJS pipes. It's crucial to unsubscribe from Observables manually when they are no longer needed to prevent memory leaks.
const subscription = combineLatest([this.jobService.getEditableText('admins', compareFile["0"].path, 1000),
this.jobService.getEditableText('admins', compareFile["2"].path, 1000)])
.subscribe(console.log)
Once both functions return results, you will see a console log with an array of results: [res1, res2].
Don't forget to unsubscribe once the subscription is no longer necessary (e.g., when destroying the component):
subscription.unsubscribe();
If you encounter any deprecation warnings, it may be due to how you're passing the observables to the combineLatest operator. combineLatest itself is not deprecated. In newer versions, pass an array of observables:
combineLatest([one$, two$])
Why not use flatMap/mergeMap
or switchMap
?
In this scenario, flatMap
(an alias of mergeMap
) or switchMap
may not be necessary since the two function calls seem independent (no dependency). You can execute them "in parallel" without waiting for one to emit before calling the other.