Sobes.tech
Junior — Middle

Can you explain what the term Observable means in the context of programming?

sobes.tech AI

Answer from AI

In programming, the term Observable refers to a design pattern that implements the concept of a "watchable" object, which other objects—observers—can monitor for changes.

Observable is a data source that can asynchronously send events or data to subscribers. Subscribers receive notifications when the state of the Observable changes.

An example in Python using the RxPy library (ReactiveX for Python):

from rx import Observable

# Create an Observable that emits numbers from 1 to 3
observable = Observable.from_([1, 2, 3])

# Subscribe to events and print them
observable.subscribe(
    on_next=lambda x: print(f"Received: {x}"),
    on_error=lambda e: print(f"Error: {e}"),
    on_completed=lambda: print("Completed")
)

Thus, Observable allows convenient working with data streams and events, especially in asynchronous programming.