select is one of the most used operators in Rx and probably the easiest to grasp.
Its intended purpose is to transform a value.
It is also known as
Examples with arrays
select act in the same way, except for with Rx, we assume its async.
Examples with Rx.
Note: Subscribe would be invoked 3 times.
To really understand the select operator, lets take apart the source code. I find that this really helps me in understanding new technology.
First here is the
Notice how select works. The select command
subscribes to the
parent (current Observable at the time of select)
and passes the
value through the provided
The selector is the function that is provided in the
Re-look at the source, specifically the following line.
Notice that the
subscribe call is made immediately when the result of the
select call is subscribed to.
So image the following situation.
What is going to happen? Stack Overflow!!! Its all about the
parent.subscribe statement. As the first subscribe happens, each
parent will be subscribed to all the way up to the original observable (
returnValue(1)). Which will generate a 100,000 stack frames. Don’t do this.
Something to think about of which will be addressed later is what if a select statement returns an observable? All the sudden you now have an Observable of Observables.