In this section, we’ll continue from the previous section and delve further. We’ll basically see how to create and observable and also how to use observable effectively without a class rather based on a function.
Hence, without wasting time, let’s get started.
Now, let me explain the code a bit. Here, first it will receive a value inside a subscribe function, similarly 2nd function which I would like to subscribe will be an error observable and then I can pass the complete handler as it doesn’t receive any parameter, hence I can pass empty parentheses to build this function and then I can simply print message there.
Therefore, this is another way, basically a simple way to build the observer. With the above approach we have actually trimmed down the code and output remains the same. Now, let’s look at another approach of creating observable. Below is the snippet for the same.
Here, you can see that each time my observable receives a new value, my observer reacts to that value or basically print that value. And below is the output for the same.
Nothing changed. However, so far we haven’t used error pattern. Let’s use that as well. Below is the snippet for the same.
Here, I have purposely terminated the flow with an error, which means as soon as it reaches to the value 7, it just landed on error handler and hence didn’t print values after 6 and not even complete cycle got called. Below is the output for the same.
Therefore, raising an error basically means you are terminating the process and control will simply come out of the program. I hope you would have liked today’s discussion. In coming section, we’ll see more around the same. Till then stay tuned and Happy Coding.
1,192 total views, 4 views today