#68 What is an Observable | Understanding Observables & RxJS | A Complete Angular Course

procademy
9 Nov 202311:07

Summary

TLDRThis section of the Angular course delves into the concept of observables, essential for handling asynchronous data in JavaScript. It contrasts observables with promises, highlighting their ability to manage data streams, unlike promises which offer a single resolution. The script explains that observables, provided by the RxJS library, use the Observer pattern to emit events that subscribers can react to. The lecture promises a practical demonstration of creating and utilizing observables in Angular applications, emphasizing their non-blocking nature and suitability for tasks like streaming large data sets.

Takeaways

  • 📚 Observables are used in Angular to handle asynchronous data, similar to how promises are used in JavaScript.
  • 🔁 JavaScript is a single-threaded language, meaning it executes code line by line, and long tasks can block subsequent code execution.
  • 🌐 Asynchronous operations, like HTTP requests, can take time and are non-blocking, allowing the main thread to continue executing other code.
  • 🚀 Promises can handle asynchronous data but are limited to returning a single value or error, unlike observables.
  • 📡 Observables can handle a stream of data, emitting multiple values over time, which is useful for applications like video streaming.
  • 🔄 The difference between promises and observables lies in their ability to handle data streaming, with observables being more suited for this purpose.
  • 🛑 Promises will always return data or an error, regardless of whether it is used, while observables will only emit data if there is a subscriber.
  • 📚 Observables are not a native feature of JavaScript or Angular but are provided by the RxJS library, which uses the Observer pattern.
  • 🔄 The Observer pattern involves an observable that emits events and an observer that listens and reacts to these events.
  • 🛠 RxJS is a library for working with asynchronous data streams, providing methods to manipulate and work with observables.
  • 🔍 The script promises to cover the creation and usage of observables in Angular applications in the next lecture.

Q & A

  • What is an observable in the context of Angular?

    -An observable is a mechanism used to handle asynchronous data in Angular. It is a way to represent a stream of data that can be observed and processed over time.

  • Why do we need observables in JavaScript, considering it already has promises?

    -While promises are useful for handling asynchronous data, observables offer more flexibility, especially when dealing with streams of data where multiple values are emitted over time, unlike promises which resolve with a single value or error.

  • How does JavaScript's single-threaded nature affect the execution of asynchronous operations?

    -JavaScript's single-threaded nature means that long-running tasks can block the execution of subsequent code. Asynchronous operations, like HTTP requests, prevent this blocking by running in the background, allowing the main thread to continue executing other code.

  • What is the difference between synchronous and asynchronous code in terms of their effect on the main thread?

    -Synchronous code is blocking; it must complete before the next line of code can execute. Asynchronous code, on the other hand, is non-blocking, allowing other operations to proceed on the main thread while waiting for the asynchronous operation to complete.

  • Can you explain the concept of streaming data as mentioned in the script?

    -Streaming data refers to the process of sending large amounts of data in small chunks or packets over time, rather than all at once. This is useful for applications like video streaming, where users can start consuming the content before the entire file has been downloaded.

  • How does a promise handle a stream of data?

    -A promise is not designed to handle a stream of data. It resolves or rejects with a single value or error when the asynchronous operation completes, making it unsuitable for scenarios where multiple data chunks are expected.

  • What is the main advantage of using observables over promises when dealing with streams of data?

    -Observables can emit multiple values over time, allowing them to handle streams of data effectively. They can emit new data as it arrives, making them ideal for applications that require real-time data updates or processing.

  • Why does an observable only provide data if there is someone to use it?

    -Observables are designed to be lazy, meaning they will only emit data when there is an observer (subscriber) listening to it. This helps in optimizing performance and resource usage by avoiding unnecessary data processing.

  • What is the relationship between observables and the RxJS library?

    -Observables are not a native feature of JavaScript or Angular; they are provided by the RxJS library, which stands for 'Reactive Extensions for JavaScript'. RxJS offers a suite of tools and methods for working with observables and asynchronous data streams.

  • Can you briefly describe the Observer pattern as it relates to observables?

    -The Observer pattern involves an observable (or subject) that emits events and one or more observers (subscribers) that listen for these events. When an event is emitted, the observers can react to it by executing specific logic or functions, such as handling data with the 'next' callback, or responding to errors or completion with their respective callbacks.

  • What are the types of events that an observable can emit in RxJS?

    -In RxJS, an observable can emit three types of events: 'next' for emitting data, 'error' for signaling an error condition, and 'completion' to indicate that the observable has finished emitting data.

Outlines

00:00

📚 Introduction to Observables in Angular

This paragraph introduces the concept of observables in Angular and their role in handling asynchronous data. It compares observables with promises, which are JavaScript's built-in way of dealing with asynchronous operations. The explanation clarifies that while JavaScript is single-threaded and synchronous code can block execution, asynchronous operations like HTTP requests can be managed without blocking the main thread. The paragraph sets the stage for a deeper dive into observables, their differences from promises, and their use in streaming data scenarios.

05:02

🔄 Understanding Data Streaming and Observables vs Promises

The second paragraph delves into the concept of data streaming, where large data sets are sent in small chunks to improve efficiency and user experience. It contrasts this with the traditional method of sending all data at once. The main difference between promises and observables is highlighted here: while promises provide a single value or error, observables can handle multiple values or streams of data over time. Observables are particularly useful for applications like video streaming, where data is sent in packets. The paragraph also explains that observables are part of the RxJS library and not a native JavaScript feature, and introduces the observer pattern, which is foundational to how observables work.

10:03

🛠 RxJS Library and Observables in Practice

The final paragraph discusses the RxJS library, which provides a suite of methods for working with observables and asynchronous data streams. It emphasizes that observables are not a native part of JavaScript or Angular but are instead provided by RxJS. The paragraph outlines the observer pattern, where an observable emits events and an observer listens and responds to these events. It also touches on the types of events that an observable can emit: next, error, and completion. The summary mentions that the practical application of these concepts will be covered in upcoming lectures, promising a deeper exploration of how to create and work with observables in Angular applications.

Mindmap

Keywords

💡Observable

Observables are a core concept in the video, representing a way to handle asynchronous data streams in programming. Defined as a mechanism to deal with data that becomes available over time, observables are integral to the theme of asynchronous operations in JavaScript and Angular. In the script, observables are compared with promises and are highlighted as a means to manage streams of data, such as in a video streaming application, where data can be sent in chunks rather than all at once.

💡Asynchronous Data

Asynchronous data refers to data that is not immediately available but will be at some point in the future. It is central to the video's theme as it explains how JavaScript, being a single-threaded language, deals with operations that take time to complete without blocking the main thread. The script uses the example of an HTTP request to illustrate how asynchronous data is handled in Angular using either promises or observables.

💡Promise

A promise in JavaScript is a built-in feature used to handle asynchronous operations. It represents a value that may be available now, later, or never. The video script contrasts promises with observables, noting that while a promise can handle asynchronous data, it is not designed to handle streams of data and will resolve once the first piece of data is received, unlike observables which can emit multiple values over time.

💡Single-threaded Programming Language

The term 'single-threaded programming language' is used in the script to describe JavaScript's execution model, where code is executed line by line in the order it is written. This concept is fundamental to understanding asynchronous operations, as it explains why operations like HTTP requests are handled without blocking the main execution thread, a key point in the video's discussion on asynchronous data handling.

💡HTTP Request

HTTP Request is a method used in the script to exemplify how asynchronous operations work in a single-threaded environment like JavaScript. It is a request made to a server to fetch or send data, and it is used to illustrate the concept of non-blocking code, where the main thread continues to execute subsequent code while waiting for the server's response.

💡Streaming of Data

Streaming of data is a concept where data is sent in small chunks over time rather than all at once. The script uses this concept to differentiate between promises and observables, explaining that while a promise is not suitable for streaming because it resolves with the first piece of data, observables are ideal for such scenarios, allowing for the handling of multiple chunks of data over time.

💡RxJS

RxJS, which stands for Reactive Extensions for JavaScript, is a library that provides observables and other functionalities for handling asynchronous data streams. The script mentions RxJS as the source of observables in Angular, emphasizing that it is not a native feature of JavaScript but rather a powerful tool provided by this external library for managing complex data flows.

💡Observer Pattern

The Observer pattern is a software design pattern that the script introduces as the basis for observables in RxJS. It involves an object, known as the subject or observable, maintaining a list of dependents, called observers or subscribers, and automatically notifying them of any state changes. The script explains this pattern in the context of handling events emitted by observables in Angular applications.

💡Subscriber

A subscriber in the context of the video is an entity that listens to and reacts to events emitted by an observable. It is part of the Observer pattern and is used to handle the data or events as they are emitted. The script explains that subscribers can execute callbacks in response to next, error, or completion events emitted by an observable.

💡Data Stream

A data stream in the video refers to a sequence of data points that are emitted over time. It is a fundamental concept when discussing observables, as observables are used to handle and transform these streams of data. The script uses the term to describe how observables can be used to manage continuous flows of data, such as in streaming applications.

Highlights

Observables are used to handle asynchronous data in Angular.

JavaScript's single-threaded nature and the impact on code execution.

Asynchronous operations prevent blocking of the main thread in JavaScript.

Promises vs Observables in handling asynchronous data in Angular.

Explanation of synchronous code's blocking nature in JavaScript.

The concept of streaming data in applications like video streaming services.

Promises provide a single value or error upon completion of an asynchronous operation.

Observables can handle streams of data, emitting multiple values over time.

Observables are conditional, emitting data only when there is a subscriber.

Promises are a native JavaScript feature, unlike Observables.

RxJS library provides Observables, which are not part of native JavaScript or Angular.

Introduction to the Observer pattern as the basis for Observables.

Observables emit 'next', 'error', and 'completion' events in RxJS.

Subscribers listen to Observable events and handle them with callback functions.

RxJS provides methods to work with and manipulate data streams from Observables.

Upcoming lectures will cover practical creation and usage of Observables with RxJS in Angular.

The importance of understanding Observables for managing asynchronous operations in Angular applications.

Transcripts

play00:01

[Music]

play00:09

hello and welcome to another section of

play00:10

this complete angular course in this

play00:12

section we will try to understand what

play00:14

is an observable and where and when do

play00:17

we use it and let's start this section

play00:19

by understanding what is an observable

play00:22

and what do we use it

play00:23

for in very simple term we can say that

play00:27

we use observables to handle

play00:28

asynchronous data

play00:30

now we can also use promises to handle

play00:32

asynchronous data promise is a built-in

play00:35

feature of JavaScript which we can use

play00:37

to handle asynchronous data so we can

play00:40

handle asynchronous data in angular

play00:42

either by using an observable or a

play00:44

promise so before we proceed further

play00:47

let's quickly try to understand what is

play00:49

an asynchronous operation and

play00:51

asynchronous

play00:52

data we already know that JavaScript is

play00:55

a single threaded programming language

play00:56

that simply means that in JavaScript the

play00:59

code is executed Ed line by line in the

play01:00

order in which they are defined and once

play01:03

the execution of one code is complete

play01:05

then only the next line of code in the

play01:06

program will be executed so if a code or

play01:10

task takes long time in its execution

play01:12

then the next line of code will have to

play01:14

wait for its execution it will be

play01:16

executed only after the previous code

play01:19

execution is complete for example let's

play01:21

say we are making an HTTP request to the

play01:23

server to get some data now the data

play01:26

will come as a response from the server

play01:29

this http request and response cycle it

play01:31

might take some time to complete so if

play01:33

we making an HTTP request by writing

play01:35

some synchronous code the next line of

play01:37

code after HTTP request will be executed

play01:40

only after we have received the response

play01:42

from the

play01:43

server so we can say that synchronous

play01:46

code is blocking in nature and this is

play01:49

where asynchronous programming comes

play01:51

into

play01:52

picture as we learned JavaScript is a

play01:55

single threaded programming language and

play01:57

an asynchronous code does not get exec

play01:59

executed in the single thread the

play02:02

synchronous code will be executed in the

play02:04

single thread which JavaScript provides

play02:06

but asynchronous code it will not get

play02:08

executed in that single thread instead

play02:11

it gets executed in the background and

play02:13

that's why when we run an asynchronous

play02:15

code it does not block the main thread

play02:18

so an asynchronous code is non-blocking

play02:21

that means when we make an HTTP request

play02:23

asynchronously it will run in the

play02:25

background and the next code after HTTP

play02:28

request will be executed immediately in

play02:30

the main thread and in this way the HTTP

play02:33

request will not block the execution of

play02:35

next line of code so using a synchronous

play02:38

programming we can perform time

play02:40

consuming Network requests without

play02:42

blocking the main thread now an

play02:44

asynchronous code will return us some

play02:46

data after some time so we need to wait

play02:48

for that data and once the data is

play02:51

available we can utilize it in our code

play02:54

and to handle that asynchronous data we

play02:56

use either a promise or an observable so

play03:00

let's Now understand the difference

play03:01

between a promise and an observable and

play03:04

to understand the difference we first

play03:06

need to understand what is streaming of

play03:09

data so here let's say we are creating

play03:11

an angular application and from that

play03:14

angular application we want to make an

play03:16

HTTP request to the server now this

play03:19

server it will get some data either from

play03:21

the database or from the web API and

play03:24

this data which we are requesting from

play03:25

the server it might be a huge data and

play03:28

we want to send the data back to the

play03:30

client through HTTP

play03:32

response now this data can be sent by

play03:35

the server to the client in two ways the

play03:38

first way is we get all the data from

play03:41

the database or web API and send all the

play03:43

data at once or what we can also do is

play03:46

we can divide this data into small

play03:48

chunks and we can send each chunk at a

play03:51

time to the client for example let's say

play03:54

we are creating a video streaming app

play03:56

like Netflix or YouTube now when the

play03:59

user makes a request from our app to the

play04:01

server requesting a video file so let's

play04:03

say from here from our client we have

play04:05

make a request to the server requesting

play04:07

a video file and from the server we want

play04:10

to send that video file to the client

play04:12

now here let's say the video file size

play04:15

is of 1GB now 1GB data is very huge and

play04:18

it might take some time to send all the

play04:20

data at once to the client so in this

play04:22

case we are sending 1 GB of file to the

play04:25

client and this file is very huge so

play04:29

here the user will have to wait for the

play04:31

complete file to get downloaded and once

play04:34

the file is completely downloaded then

play04:36

only the user can start watching the

play04:38

video so in this scenario we are sending

play04:41

all the data at once we are sending one

play04:44

big file at once to the

play04:46

client but instead of sending the

play04:48

complete data at once the server can

play04:51

send the data in small packets so here

play04:54

1GB data it can be divided into small

play04:57

packets and can be sent to the client

play04:59

one after the other in this way the user

play05:02

does not have to wait for the complete

play05:04

file to be downloaded instead he can

play05:06

start watching the video as soon as the

play05:08

first packet arrives so here we are

play05:11

streaming the data in this approach the

play05:13

data is sent to the client in small

play05:15

chunks instead of sending the data all

play05:17

at once in a big

play05:19

chunk and this is called as streaming of

play05:21

data here we are streaming a huge file a

play05:25

big file into small chunks to the

play05:28

client now that we know what streaming

play05:30

of data is let's try to understand the

play05:32

difference between a promise and an

play05:35

observable a promise promises us some

play05:38

data over a period of time the data

play05:40

which promise returns us it can be an

play05:42

actual data or it can also be an error

play05:45

so let's say we have made an HTTP

play05:47

request to the server now a promise will

play05:49

return us the response data if

play05:51

everything was okay or it will return us

play05:54

an error object if something went wrong

play05:56

for example if there was some Network

play05:58

issue then the promise will return us an

play06:00

error object but if there was no error

play06:03

and we have received the response data

play06:05

successfully in that case the promise

play06:07

will return us that response data now

play06:11

the main difference between a promise

play06:13

and an observable is that a promise

play06:15

cannot handle stream of data if we use a

play06:18

promise for handling stream of data then

play06:21

the promise will resolve as soon as the

play06:23

first chunk of data arrives after that

play06:26

when the next chunk of data arrives it

play06:27

will not handle that data

play06:30

so a promise returns us a single value

play06:32

or a single piece of data but with

play06:35

observables we can handle stream of data

play06:37

very easily and we will understand it

play06:39

with an example in the next

play06:41

lecture so an observable can return us

play06:44

multiple values it can return us

play06:46

multiple

play06:47

data another reference is that a promise

play06:51

will certainly return some data even if

play06:54

there is no code using that data so for

play06:56

example let's say we have made an HTTP

play06:58

request to the server to request some

play06:59

dat data now we going to receive that

play07:01

data but we are not going to use that

play07:04

data anywhere in our code so promise

play07:07

will return us that data even if there

play07:09

is no one to use that data but in case

play07:12

of an observable the observable will

play07:14

only provide the data if there is

play07:16

someone to use that data if there is no

play07:19

code using that data in that case the

play07:21

observable will not send that

play07:23

data this is another very important

play07:25

point to remember about observables and

play07:28

we will learn about it prac Ally in our

play07:29

coming lectures finally A promise is

play07:33

native to JavaScript it is provided by

play07:35

JavaScript language but observable it is

play07:38

not native feature of angular or

play07:39

JavaScript it is provided by another

play07:42

JavaScript library called as

play07:45

rxjs so we can say that an observable is

play07:48

a function that converts the ordinary

play07:50

data stream into an observable one and

play07:53

you can think of an observable as a

play07:55

rapper around the ordinary data

play07:58

Steam now this observable it is provided

play08:01

by rxjs library and rxjs Library uses

play08:05

Observer pattern so let's also quickly

play08:08

understand what is an observer

play08:09

pattern in an observer pattern first we

play08:13

have an eventim meter we can also call

play08:15

it as observable this observable it is

play08:18

going to emit some event and then we

play08:21

also have an observer and this Observer

play08:24

it is going to listen for that event so

play08:27

whenever an observable image Senter

play08:29

event The Observer will wait for that

play08:31

event to happen and once that event

play08:33

happens The Observer can handle that

play08:36

event using event

play08:38

handlers so in an observer pattern we

play08:42

have an observable you can also call it

play08:43

as eventer which is going to emit sub

play08:45

event then we have an observer you can

play08:48

also call it as event listener or

play08:50

subscriber which is going to listen for

play08:52

those events and whenever that event

play08:55

happens The Observer can handle that

play08:57

event it can execute some law logic with

play09:00

the help of event

play09:01

handler now in rxjs an observable emits

play09:05

these following events so it can emit a

play09:08

next event an error event or a

play09:10

completion event and there will be an

play09:13

observer which will subscribe to these

play09:15

events it will subscribe to an

play09:17

observable and whenever that observable

play09:19

emits the next event or error event or

play09:21

completion event the subscriber is going

play09:24

to listen for that event and whenever

play09:27

that event happens if the subscriber

play09:29

wants it can handle that event by

play09:32

executing some

play09:33

code and it can handle that event by

play09:36

passing this next function error

play09:39

function or completion function as a

play09:41

call back function to the Subscribe

play09:44

method now if this does not make any

play09:46

sense right now don't worry we are going

play09:48

to understand it practically in our next

play09:50

lecture but keep in mind that when we

play09:53

use an observable it is basically going

play09:55

to use the Observer pattern where an

play09:57

observable will emit an event whenever

play09:59

something happens and the Observer or we

play10:02

can also call it as subscriber it will

play10:04

listen for that event and when that

play10:06

event happens the subscriber can handle

play10:08

that event by executing some

play10:12

functions now as I mentioned earlier

play10:15

observable is not native to JavaScript

play10:16

or angular it is provided by another

play10:18

JavaScript library called as rxjs so the

play10:21

rxjs which stands for reactive extension

play10:24

library for JavaScript it is a

play10:26

JavaScript library that allows us to

play10:28

work with asyn inous data streams the

play10:31

rxjs library it provides us with a lot

play10:33

of methods which we can use to work with

play10:35

observables and manipulate the data

play10:38

which the observable is going to

play10:40

Emit and we are going to talk about some

play10:42

of these methods of rxjs library in our

play10:44

coming

play10:45

lectures so this is all from this

play10:47

lecture in the next lecture let's learn

play10:50

how we can create an observable using

play10:52

rxjs library and how we can work with an

play10:55

observable in our angular

play10:58

application

Rate This

5.0 / 5 (0 votes)

相关标签
AngularObservablesAsynchronousData StreamsJavaScriptPromisesRxJSHTTP RequestsEvent EmitterObserver Pattern
您是否需要英文摘要?