I seem to run into this situation a lot where I am needing to pass data along a chain of streams. In other words, I have obervables that are dependent on the output of one or more other observables.
Below are 3 ways to do this with a simple example, but none of them feel like the "RxJS" way. Is there a better way to do this?
// Method #1 - looks clean, but feels hacky
let firstResponse = null;
performFirstAction().pipe(
tap(_firstResponse => (firstResponse = _firstResponse)),
switchMap(_firstResponse => performSecondAction(_firstResponse)),
switchMap(secondResponse => performThirdAction(firstResponse, secondResponse))
);
// Method #2 - gets ugly real quick as it scales
performFirstAction().pipe(
switchMap(firstResponse =>
performSecondAction(firstResponse).pipe(
map(secondResponse => ({ firstResponse, secondResponse }))
)
),
switchMap(({ firstResponse, secondResponse }) =>
performThirdAction(firstResponse, secondResponse)
)
);
// Method #3 - might as well just use callbacks at this point
performFirstAction().pipe(
switchMap(firstResponse =>
performSecondAction(firstResponse).pipe(
switchMap(secondResponse =>
performThirdAction(firstResponse, secondResponse)
)
)
)
);