What is the difference between Observables and regular JavaScript events?

Question:

I've seen a lot of people talking about Observables and the Observer pattern in general, mainly from the Rx JS library. What's the difference between Observers and the events we tend to handle like onclick , onkeypress , onkeydown , etc? If we already have the ability to create and respond to events, why do we need "artificial" Observers? Wouldn't the function of the two be the same?

Answer:

The main difference between the two is that an Observer is for when something changes , whereas an Event is for when something happens .

The practical example of this would be, in a todo list:

The function associated with the "add to list" button sends an event saying "add X to list"; something receives this event and adds the X to the list; When this list changes, it triggers a Subscriber to all observers with the necessary information (usually the new date).

In other words: One does not replace the other and can be used side by side.

Scroll to Top
AllEscort