Skip to content
Advertisement

RxJS observable of results from chain of functions

I have a function getC which depends on the output of getB which depends on the output of getA. I want to create an observable stream, so that when I subscribe to it I get the output of each function, as shown in the snippet.

Is there an operator in RxJS that will give me the same behaviour as in the snippet? Or is nesting switchMap, concat and of the only way?

JavaScript
JavaScript

Advertisement

Answer

If you know your dependencies in advance you can hardcode them in the different streams:

  1. c$ requires the latest emission from b$
  2. b$ requires the latest emission from a$
  3. Run the streams in “order” whilst respecting their dependencies so you end up with three emissions, each (except for the first) depending on the previous one.

JavaScript
JavaScript
Advertisement