question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple subscrptions on single request call create multiple requests

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ x ] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

When having multiple subscriptions on an observable returned from an http.post request the request function is only called once but the request is executed once for each subscriptions

Expected behavior

each call to http.post function results in exactly one request

Minimal reproduction of the problem with instructions

In a clean angular app use this component: (add HttpModule to app.module)

import { Component } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  count = 0;

  constructor(private http: Http) {
    const o = this.request();
    o.subscribe(() => console.log('foo'), () => console.log('foo'));
    o.subscribe(() => console.log('bar'), () => console.log('bar'));
  }

  request(): Observable<any> {
    console.log('request ' + this.count++);
    return this.http.post('https://foo.bar', {});
  }
}

Inspect the page and reload to see network activity.

Although the request() function has only been called once the request is actually executed twice

What is the motivation / use case for changing the behavior?

Environment


Angular version: 5.6.0 and 4.4.6


Browser:
- [x ] Chrome (desktop) version 63.0.3239.132
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ x ] Firefox version 52 ESR
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
Others:

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
frederikprijckcommented, Feb 5, 2018

@wutscher0815 This is how rxjs (which is what http uses internally) works (btw, you want to use HttpClient instead of Http) You probably want to have a look at rxjs operators to avoid this.

Try the following to “resolve” your “issue”:

request(): Observable<any> {
    console.log('request ' + this.count++);
    return this.http.post('https://foo.bar', {}).shareReplay();
  }

Note: This is only one of the possible solutions (be sure to have a look at share, shareReplay and publishReplay), but you probably wanna dig into how rxjs works.

Nevertheless, this doesn’t have anything to do with Angular itself.

0reactions
angular-automatic-lock-bot[bot]commented, Sep 13, 2019

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular - How to call make an HTTP Call that has multiple ...
I have an angular service that calls an API and gets a list of pages. These pages are subscribed to from multiple components....
Read more >
Multiple requests on the same trigger - Zapier Community
I have done this a few times. The key is to have the first request use a synchronous call so it executes to...
Read more >
How to create multiple subscriptions for a single customer?
Under the "Subscriptions" tab, go to "Customers", open the specific customer and use the "Create New Subscription" option to create multiple subscriptions for ......
Read more >
Handle multiple API requests in Angular using mergeMap and ...
Subscribe. Using this technique is quite simple. First, we call one API to get user info, and then we call the other two...
Read more >
Combine multiple requests in one HTTP call using JSON ...
Combining these three individual requests into a single batch request can save the application significant network latency. Microsoft Graph ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found