Observable is by far the most important concept in this library. I would call it a powerful generalization of a function. I can explain what I mean.

Lets start by learning about functions.

Simple function example

If you care fully observe the above code snippet you understand that the declaration of function is Lazy. By lazy i mean the code inside the function is not executed until the function is called.

Now let’s make an Observable and compare it with a function.

We can create an Observable using Rx.Observable.create Create operator helps you create the Observable .

Now, observables have a method called subscribe . That takes a function which takes the value x . x is whatever was fed through .next . Once I have that value x , I can just put it in console.log , almost the same thing we had for functions.

Simple Observable example

What happens then if I comment out our .subscribe block? Basically, I don't have a .subscribe anymore. What will happen? Well, nothing. That means that whatever is inside an observable is also a lazy computation. Just like with functions, if you don't express your interest in the values, then nothing will happen.

So far, no difference at all.Then what is really the difference between observables and functions?

Here is one major thing, is that you can call observer.next multiple times. You can give another value, so here is .next(100) . Here is .next(200) , and now we're basically returning many values. Where as functions end after a value is returned.

Example of Observable with multiple next values.

Also Observables can also be asynchronous.Here I want you to understand that while Observables can be asynchronous, they can be synchronous as well. Let me give you an example to understand better.

Example of Observable running synchronously

Example of Observable running asynchronously

This is a part 1 of 2 Articles about Introduction to observables and RxJs.I will continue with more on creation of observables and followed operators which makes observables so powerful and easy to use.

This article series is a summary of what i learnt from André Staltz course on Egghead.