Amadou Beye
Amadou Beye

Reputation: 2828

Difference between the methods .pipe() and .subscribe() on a RXJS observable

I recently notice that I can return a value inside .pipe() but not inside .subscribe().

What is the difference between these two methods?

For example if I have this function, let's call it 'deposit', which is supposed to return the account balance, if I do this:

deposit(account, amount){
    return this.http.get('url')
    .subscribe(res => {
        return res;
    }
}

It returns an observable and if I do this:

deposit(account, amount){
    return this.http.get('url')
    .pipe(
        map(res => {
            return res;
        });
    );
}

It returns the account balance as expected.

So why?

Upvotes: 130

Views: 167006

Answers (2)

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92687

Examples

One important difference is that if you don't execute subscribe, the request will newer be sent and pipe will be never be executed. Here are working examples that show the difference:

  • Subscribe

    const { interval, of } = rxjs;
    const { delay, take } = rxjs.operators;
    
    this.http = { get: (url)=> of(url).pipe(delay(1000), take(1)) } // request simulator
    
    deposit = (account, amount) => {
        return this.http.get('url')
          .subscribe(res => {
              console.log('hello from subscriber');
              return res;
          })
    }
    
    let subscription = deposit('',''); // immediately send request
    // you can cancel request by subscription.unsubscribe()
    
    console.log('subscribed');
    <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.5/rxjs.umd.min.js" integrity="sha256-85uCh8dPb35sH3WK435rtYUKALbcvEQFC65eg+raeuc=" crossorigin="anonymous"></script>

  • Pipe

    const { interval, of,  } = rxjs;
    const { delay, take, map } = rxjs.operators;
    
    this.http = { get: (url)=> of(url).pipe(delay(1000), take(1)) } // request simulator
    
    deposit = (account, amount) => {
        return this.http.get('url')
            .pipe(
                map(res => {
                    console.log('hello from pipe');
                    return res;
                })
            );
    }
    
    const observable = deposit('',''); // this will return observable and do nothing
    
    
    console.log('nothing happen');
    <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.5/rxjs.umd.min.js" integrity="sha256-85uCh8dPb35sH3WK435rtYUKALbcvEQFC65eg+raeuc=" crossorigin="anonymous"></script>

  • Pipe + Subscribe

    const { interval, of,  } = rxjs;
    const { delay, take, map } = rxjs.operators;
    
    this.http = { get: (url)=> of(url).pipe(delay(1000), take(1)) } // request simulator
    
    deposit = (account, amount) => {
        return this.http.get('url')
          .pipe(
              map(res => {
                  console.log('hello from pipe');
                  return res;
              })
          );
    }
    
    const observable = deposit('',''); // this will return observable and do nothing
    
    const subscription = observable.subscribe(result => { // this will send request 
      console.log('hello from subscriber')
    }); 
    
    // subscription.unsubscribe() - this will cancel request
    <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.5/rxjs.umd.min.js" integrity="sha256-85uCh8dPb35sH3WK435rtYUKALbcvEQFC65eg+raeuc=" crossorigin="anonymous"></script>

Upvotes: 14

Reactgular
Reactgular

Reputation: 54821

The pipe method is for chaining observable operators, and the subscribe is for activating the observable and listening for emitted values.

The pipe method was added to allow webpack to drop unused operators from the final JavaScript bundle. It makes it easier to build smaller files.

For example if I have this function, let's call it 'deposit', which supposed to return the account balance, if I do this:

deposit(account, amount){
    return this.http.get('url')
    .subscribe(res => {
        return res;
    }
}

It returns an observable

That isn't what it returns. It returns the Subscription object created when you called Subscribe.

and if I do this:

deposit(account, amount){
    return this.http.get('url')
    .pipe(
        map(res => {
            return res;
        });
    );
}

It returns the account balance as expected.

That isn't what it returns. It returns an Observable which uses a map operator. The map operator in your example does nothing.

Upvotes: 119

Related Questions