SlideShare a Scribd company logo
Jalpesh Vadgama
@jalpesh, Co-Founder, FutureStack Solution
Top 10 RxJs
Operators in Angular
About Me
• More than 14 years of experience in Web Development,
Cloud Solutions and Enterprise Grade Applications.
• Co-Founder of Future Stack Solution
http://www.futurestacksolution.com
• Awarded Microsoft MVP 8 times. Currently MVP in Visual
Studio and Development Technologies.
• A community guy love to network with new people!.
• An enthusiast developer trying to learn everyday
something new
Agenda
• What problems RxJs going to solve?
• Callback, Promises and What is the problem with that?
• Observables
• Demo with Observables and Of operator
• Different operators available in RxJs
• Questions and Answer
“To think creatively, we must be
able to look afresh at what we
normally take for granted”
- George Keller
Bringing functional
programming to
JavaScript
Callbacks
functionToCallXHR() {
function Success(successParams) {
// Handle success
}
function Failure(failureParams) {
// Handle failure
}
}
Promises
let result = http.get("your api url");
result.then(function(params){
},
function (params){
// Handle failure
}
);
// Handle success
What is I want to retry again
after failure?
Observables
What is Observables?
An observable is essentially a stream (a stream of events, or
data) and compared to a Promise, an Observable can be
cancelled..
What is Observables?
An observable have at least participant
The Creator
The Subscriber:
result.subscribe(){
next => {handleYourEmittedValue(next)},
console.error,
()=> console.log("All finished");
}
let result = http.get("your api url");
Operators
• Operators are functions that operate on observables and returns the
new observables
• They are pure functions that transform information in the observable
stream that create new observables, often based on the current
observable
• Most importantly, the operators in RxJS allow for complex
asynchronous code that can be easily composed in a declarative
manner.
.
Operator Demo
Of operator
• Of operators convert any object into observables
• Mostly we are using it from to convert a JSON retrieve from our apis
Concat operator
• Concat operator emit the emissions from two or more Observables
without interleaving them
• You can think of concat like a line at a ATM, the next transaction
(subscription) cannot start until the previous completes!
// Simulate HTTP requests
const getPostOne$ = Rx.Observable.timer(3000).mapTo({ id: 1 });
const getPostTwo$ = Rx.Observable.timer(1000).mapTo({ id: 2 });
Rx.Observable.concat(getPostOne$, getPostTwo$)
.subscribe(res => console.log(res));
MergeMap operator
• The merge map operator is handy when the requirement is to
merge response from two observables
const firstNameObs$ = of('Jalpesh');
const lastNameObs$ = of('Vadgama');
const finalObs$ = firstNameObs$.pipe(
mergeMap(event1 => lastNameObs$.pipe(
map(event2 => event1 + ' ' + event2)))
);
const subscription = finalObs$.subscribe((value) => console.log(value));
// outputs: Jalpesh Vadgama
Filter operator
• The filter operator is handy when you need to filter something
• The following example will get an observable that emit the numbers.
We will use the filter operator to only emit the even numbers
this.numberService.getNumbers().pipe(
filter(n => n % 2 === 0),
)
.subscribe(result => console.log(result));
Delay operator
• The Delay operator emits values after some delay
const sub: Subject<number> = new Subject<number>();
sub.pipe(
delay(500)
).subscribe((val: number) => {
console.log(val);
});
sub.next(0);
sub.next(1);
sub.next(2);
// after 500ms delay will emit all the val
Retry operator
• if a source Observable emits an error, resubscribe to it in the hopes
that it will complete without error
getBook(id: number): Observable < Book > {
let url = this.bookUrl + "/" + id;
return this.http.get<Book>(url).pipe(
retry(3),
// retry the failed request up to 3 times
catchError(err => {
console.log(err);
return of(null);
})
);
}
Zip operator
• Multiple observables emitting at alternate intervals
const sourceOne = of('Hello');
const sourceTwo = of('World!');
const sourceThree = of('Goodbye');
const sourceFour = of('World!');
//wait until all observables have emitted a value
then emit all as an array
const example = zip(
sourceOne,
sourceTwo.pipe(delay(1000)),
sourceThree.pipe(delay(2000)),
sourceFour.pipe(delay(3000))
);
//output: ["Hello", "World!", "Goodbye", "World!"]
Distinct operator
• Returns an Observable that emits all items emitted by the source
Observable that are distinct by comparison from previous items.
of(1, 1, 2, 2, 2, 1, 2, 3, 4, 3, 2, 1).pipe(
distinct(),
)
.subscribe(x => console.log(x));
// Output: 1, 2, 3, 4
IsEmpty operator
• Emits false if the input observable emits any values, or emits true if the input
observable completes without emitting any values.
const source = new Subject<string>();
const result = source.pipe(isEmpty());
source.subscribe(x => console.log(x));
result.subscribe(x => console.log(x));
source.next('a');
source.next('b');
source.next('c');
source.complete();
// Results in:
// a
// false
// b
// c
10th Operator
• There are more than 50 operators are there.
• You can look at those operators at - https://rxjs-
dev.firebaseapp.com/api/operators/
• Even you can build your own operators!!
Q&A
Thank you!
• Email: jalpesh@futurestacksolution.com
• Blog: https://www.dotnetjalps.com
• YouTube: http://bit.ly/codewithjv
• GitHub: https://github.com/jalpeshvadgama
• Twitter: @Jalpesh

More Related Content

PDF
RxJS Operators - Real World Use Cases (FULL VERSION)
PDF
Angular Observables & RxJS Introduction
PDF
Angular & RXJS: examples and use cases
PPTX
Introduction to RxJS
PDF
Angular and The Case for RxJS
PDF
Introduction to RxJS
PDF
RxJS Evolved
PDF
Angular 2 observables
RxJS Operators - Real World Use Cases (FULL VERSION)
Angular Observables & RxJS Introduction
Angular & RXJS: examples and use cases
Introduction to RxJS
Angular and The Case for RxJS
Introduction to RxJS
RxJS Evolved
Angular 2 observables

What's hot (20)

PPTX
PPTX
Intro to React
PDF
TypeScript Introduction
PPTX
[Final] ReactJS presentation
PDF
Angular - Chapter 1 - Introduction
PDF
Basics of React Hooks.pptx.pdf
PPTX
Understanding react hooks
PDF
JavaScript Fetch API
PDF
Workshop 21: React Router
PDF
Angular
PDF
React Router: React Meetup XXL
PDF
Angular components
PPTX
Introduction to React JS for beginners | Namespace IT
PPTX
Angular kickstart slideshare
PDF
React JS - Introduction
PPTX
React js programming concept
PPTX
PPTX
React JS - A quick introduction tutorial
PPTX
React workshop presentation
Intro to React
TypeScript Introduction
[Final] ReactJS presentation
Angular - Chapter 1 - Introduction
Basics of React Hooks.pptx.pdf
Understanding react hooks
JavaScript Fetch API
Workshop 21: React Router
Angular
React Router: React Meetup XXL
Angular components
Introduction to React JS for beginners | Namespace IT
Angular kickstart slideshare
React JS - Introduction
React js programming concept
React JS - A quick introduction tutorial
React workshop presentation
Ad

Similar to Top 10 RxJs Operators in Angular (20)

PPTX
RxJS In-Depth - AngularConnect 2015
PPTX
ECMAScript 6 and the Node Driver
PDF
Presto anatomy
PPTX
The Road To Reactive with RxJava JEEConf 2016
PDF
Building Scalable Stateless Applications with RxJava
KEY
Writing robust Node.js applications
PPTX
Getting Reactive with Cycle.js and xstream
PDF
RxJS Operators - Real World Use Cases - AngularMix
PDF
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
PDF
RxJS101 - What you need to know to get started with RxJS tomorrow
PDF
Clojure and the Web
PPTX
Sharding and Load Balancing in Scala - Twitter's Finagle
PDF
Performance measurement methodology — Maksym Pugach | Elixir Evening Club 3
PDF
Intro to Asynchronous Javascript
PDF
[245] presto 내부구조 파헤치기
PDF
Unit Testing Express and Koa Middleware in ES2015
PDF
Douglas Crockford: Serversideness
KEY
JavaScript Growing Up
PDF
State management in a GraphQL era
PPT
Server side JavaScript: going all the way
RxJS In-Depth - AngularConnect 2015
ECMAScript 6 and the Node Driver
Presto anatomy
The Road To Reactive with RxJava JEEConf 2016
Building Scalable Stateless Applications with RxJava
Writing robust Node.js applications
Getting Reactive with Cycle.js and xstream
RxJS Operators - Real World Use Cases - AngularMix
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
RxJS101 - What you need to know to get started with RxJS tomorrow
Clojure and the Web
Sharding and Load Balancing in Scala - Twitter's Finagle
Performance measurement methodology — Maksym Pugach | Elixir Evening Club 3
Intro to Asynchronous Javascript
[245] presto 내부구조 파헤치기
Unit Testing Express and Koa Middleware in ES2015
Douglas Crockford: Serversideness
JavaScript Growing Up
State management in a GraphQL era
Server side JavaScript: going all the way
Ad

More from Jalpesh Vadgama (7)

PPTX
Angular Ivy- An Overview
PPTX
Building Real time Application with Azure SignalR Service
PPTX
Introduction to Active Directory
PPTX
Visual Studio 2017 and ASP.NET Core 1.1
PPTX
Introduction to BOT Framework- Global Azure Bootcamp 2017
PPTX
Introduction to document db- Global Azure Bootcamp 2016
PPTX
Ahmedabad- Global Azure bootcamp- Azure Storage Services- Global Azure Bootca...
Angular Ivy- An Overview
Building Real time Application with Azure SignalR Service
Introduction to Active Directory
Visual Studio 2017 and ASP.NET Core 1.1
Introduction to BOT Framework- Global Azure Bootcamp 2017
Introduction to document db- Global Azure Bootcamp 2016
Ahmedabad- Global Azure bootcamp- Azure Storage Services- Global Azure Bootca...

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
cuic standard and advanced reporting.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPT
Teaching material agriculture food technology
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Review of recent advances in non-invasive hemoglobin estimation
Big Data Technologies - Introduction.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Agricultural_Statistics_at_a_Glance_2022_0.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectral efficient network and resource selection model in 5G networks
Encapsulation_ Review paper, used for researhc scholars
Reach Out and Touch Someone: Haptics and Empathic Computing
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
cuic standard and advanced reporting.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Teaching material agriculture food technology
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Unlocking AI with Model Context Protocol (MCP)
Building Integrated photovoltaic BIPV_UPV.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
NewMind AI Weekly Chronicles - August'25 Week I
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
The AUB Centre for AI in Media Proposal.docx
Review of recent advances in non-invasive hemoglobin estimation

Top 10 RxJs Operators in Angular

  • 1. Jalpesh Vadgama @jalpesh, Co-Founder, FutureStack Solution Top 10 RxJs Operators in Angular
  • 2. About Me • More than 14 years of experience in Web Development, Cloud Solutions and Enterprise Grade Applications. • Co-Founder of Future Stack Solution http://www.futurestacksolution.com • Awarded Microsoft MVP 8 times. Currently MVP in Visual Studio and Development Technologies. • A community guy love to network with new people!. • An enthusiast developer trying to learn everyday something new
  • 3. Agenda • What problems RxJs going to solve? • Callback, Promises and What is the problem with that? • Observables • Demo with Observables and Of operator • Different operators available in RxJs • Questions and Answer
  • 4. “To think creatively, we must be able to look afresh at what we normally take for granted” - George Keller Bringing functional programming to JavaScript
  • 6. functionToCallXHR() { function Success(successParams) { // Handle success } function Failure(failureParams) { // Handle failure } }
  • 8. let result = http.get("your api url"); result.then(function(params){ }, function (params){ // Handle failure } ); // Handle success
  • 9. What is I want to retry again after failure?
  • 11. What is Observables? An observable is essentially a stream (a stream of events, or data) and compared to a Promise, an Observable can be cancelled..
  • 12. What is Observables? An observable have at least participant The Creator The Subscriber: result.subscribe(){ next => {handleYourEmittedValue(next)}, console.error, ()=> console.log("All finished"); } let result = http.get("your api url");
  • 13. Operators • Operators are functions that operate on observables and returns the new observables • They are pure functions that transform information in the observable stream that create new observables, often based on the current observable • Most importantly, the operators in RxJS allow for complex asynchronous code that can be easily composed in a declarative manner. .
  • 15. Of operator • Of operators convert any object into observables • Mostly we are using it from to convert a JSON retrieve from our apis
  • 16. Concat operator • Concat operator emit the emissions from two or more Observables without interleaving them • You can think of concat like a line at a ATM, the next transaction (subscription) cannot start until the previous completes! // Simulate HTTP requests const getPostOne$ = Rx.Observable.timer(3000).mapTo({ id: 1 }); const getPostTwo$ = Rx.Observable.timer(1000).mapTo({ id: 2 }); Rx.Observable.concat(getPostOne$, getPostTwo$) .subscribe(res => console.log(res));
  • 17. MergeMap operator • The merge map operator is handy when the requirement is to merge response from two observables const firstNameObs$ = of('Jalpesh'); const lastNameObs$ = of('Vadgama'); const finalObs$ = firstNameObs$.pipe( mergeMap(event1 => lastNameObs$.pipe( map(event2 => event1 + ' ' + event2))) ); const subscription = finalObs$.subscribe((value) => console.log(value)); // outputs: Jalpesh Vadgama
  • 18. Filter operator • The filter operator is handy when you need to filter something • The following example will get an observable that emit the numbers. We will use the filter operator to only emit the even numbers this.numberService.getNumbers().pipe( filter(n => n % 2 === 0), ) .subscribe(result => console.log(result));
  • 19. Delay operator • The Delay operator emits values after some delay const sub: Subject<number> = new Subject<number>(); sub.pipe( delay(500) ).subscribe((val: number) => { console.log(val); }); sub.next(0); sub.next(1); sub.next(2); // after 500ms delay will emit all the val
  • 20. Retry operator • if a source Observable emits an error, resubscribe to it in the hopes that it will complete without error getBook(id: number): Observable < Book > { let url = this.bookUrl + "/" + id; return this.http.get<Book>(url).pipe( retry(3), // retry the failed request up to 3 times catchError(err => { console.log(err); return of(null); }) ); }
  • 21. Zip operator • Multiple observables emitting at alternate intervals const sourceOne = of('Hello'); const sourceTwo = of('World!'); const sourceThree = of('Goodbye'); const sourceFour = of('World!'); //wait until all observables have emitted a value then emit all as an array const example = zip( sourceOne, sourceTwo.pipe(delay(1000)), sourceThree.pipe(delay(2000)), sourceFour.pipe(delay(3000)) ); //output: ["Hello", "World!", "Goodbye", "World!"]
  • 22. Distinct operator • Returns an Observable that emits all items emitted by the source Observable that are distinct by comparison from previous items. of(1, 1, 2, 2, 2, 1, 2, 3, 4, 3, 2, 1).pipe( distinct(), ) .subscribe(x => console.log(x)); // Output: 1, 2, 3, 4
  • 23. IsEmpty operator • Emits false if the input observable emits any values, or emits true if the input observable completes without emitting any values. const source = new Subject<string>(); const result = source.pipe(isEmpty()); source.subscribe(x => console.log(x)); result.subscribe(x => console.log(x)); source.next('a'); source.next('b'); source.next('c'); source.complete(); // Results in: // a // false // b // c
  • 24. 10th Operator • There are more than 50 operators are there. • You can look at those operators at - https://rxjs- dev.firebaseapp.com/api/operators/ • Even you can build your own operators!!
  • 25. Q&A
  • 26. Thank you! • Email: [email protected] • Blog: https://www.dotnetjalps.com • YouTube: http://bit.ly/codewithjv • GitHub: https://github.com/jalpeshvadgama • Twitter: @Jalpesh