Last updated Jun 7, 2021

Concat and Merge Operator

In this tutorial, we will learn about from Concat and Merge operator of RXJS.

Concat and Merge Operator

In this tutorial, we will learn about from Concat and Merge operator of RXJS.

Concat Operator:

Concat Subscribe to observables in order as previous completes

You can think of concat like a line at an ATM, the next transaction (subscription) cannot start until the previous complete!

Example:

 const source = interval(1000).pipe(
      map((v) => "Tech Video #" + (v + 1)),
      take(5)
    );
    const source2 = interval(1000).pipe(
      map((v) => "Comedy Video #" + (v + 1)),
      take(3)
    );
    const source3 = interval(1000).pipe(
      map((v) => "News Video #" + (v + 1)),
      take(4)
    );

    const finalObs = concat(source, source2, source3);

    finalObs.subscribe((res) => {
      console.log(res);
    });

It will Output:

Tech Video #1
Tech Video #2
Tech Video #3
Tech Video #4
Tech Video #5
Comedy Video #1
Comedy Video #2
Comedy Video #3
News Video #1
News Video #2
News Video #3
News Video #4

Merge Operator:

Merge Turn multiple observables into a single observable

If throughput, not order, is a primary concern, try merge instead!

    const source = interval(3000).pipe(
      map((v) => "Tech Video #" + (v + 1)),
      take(5)
    );
    const source2 = interval(4000).pipe(
      map((v) => "Comedy Video #" + (v + 1)),
      take(3)
    );
    const source3 = interval(3500).pipe(
      map((v) => "News Video #" + (v + 1)),
      take(4)
    );

    const finalObs = merge(source, source2, source3);

    finalObs.subscribe((res) => {
      console.log(res);
      this._desingUtility.print(res, "elemet1");
    });

Output:

Tech Video #1
News Video #1
Comedy Video #1
Tech Video #2
News Video #2
Comedy Video #2
Tech Video #3
News Video #3
Tech Video #4
Comedy Video #3
News Video #4
Tech Video #5

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

Comments

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

Loading comments…

Join the discussion

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