Last updated Jun 7, 2021

What is CombineLatest operator- RXJS

In this tutorial, we will learn about combineLatest and withLatestFrom functions of RXJS.

What is CombineLatest operator- RXJS

In this tutorial, we will learn about combineLatest and withLatestFrom functions of RXJS.

CombineLatest function

The CombineLatest function combines multiple observables to create an observable, those values are calculated from the latest values of each of its input observables, once any of them emit irrespective of their index.

Suppose we have two dropdown name and color and we want to perform any operation once both of them get selected once, let see how we can do this using combinelatest function

 const nameObs = fromEvent<any>(this.name.nativeElement, "change").pipe(
      map((event) => event.target.value)
    );

const colorObs = fromEvent<any>(this.color.nativeElement, "change").pipe(
      map((event) => event.target.value)
 ); 

combineLatest(nameObs, colorObs).subscribe(([name, color]) => {
      console.log(name, color);
 });

The above example will only run when both of the dropdowns will get value and after that, any of both values changes it will subscribed

withLatestFrom function

The withlatestFrom function combine the source observable with other observables to create a result observable. Those values are calculated from the latest values of each, only when the source emits so source emission is the trigger.

withLatestFrom works as a master and slave, so it will only run when master observable changed.

Example:

 const nameObs = fromEvent<any>(this.name.nativeElement, "change").pipe(
      map((event) => event.target.value)
    );

const colorObs = fromEvent<any>(this.color.nativeElement, "change").pipe(
      map((event) => event.target.value)
 ); 

nameObs.pipe(withLatestFrom(colorObs)).subscribe(([name, color]) => {
      console.log(name, color);
    });

In Above example, it will only run when name dropdown changed.

Tags

Available for projectsLet’s build together

Need this built properly,
not just explained?

I take on Angular and NestJS work directly — auth systems, multi-tenant SaaS, API rewrites and the deploy pipeline underneath them. You talk to the person writing the code.

Projects delivered
20+ Projects delivered
Angular • NestJS
7+ years Angular • NestJS
Average reply time
1 day Average reply time

What I can help with

  • Auth and permissions systems
  • Multi-tenant SaaS foundations
  • API design and rewrites
  • Deploy pipelines and VPS setup
  • Bug fixing and performance optimization
Send me a message

Tell me about your project and I’ll get back with a realistic scope and price.

Turn
Your Ideas
Into Reality

Stay in the loop

Get the finished tutorials, projects and resources in your inbox.

Join 5,000+ developers and get new tutorials, source code, product updates and exclusive content — straight to your inbox.

  • No spam
  • Useful content only
  • Unsubscribe anytime

Subscribe to TutsCoder

Be the first to get new tutorials, projects and tips.

Join 5,000+ developers already learning with us.

Learn
Build
Grow

Discussion

0 comments

Share your thoughts, ask questions or suggest improvements. Let’s learn and grow together!

No comments yet — be the first to start the discussion.

Join the discussion

Sign in to join the discussion. Your account also keeps your saved tutorials and path progress.