Multiple subscrptions on single request call create multiple requests
See original GitHub issueI’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:
- Created 6 years ago
- Reactions:3
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@wutscher0815 This is how rxjs (which is what
http
uses internally) works (btw, you want to use HttpClient instead ofHttp
) You probably want to have a look at rxjs operators to avoid this.Try the following to “resolve” your “issue”:
Note: This is only one of the possible solutions (be sure to have a look at
share
,shareReplay
andpublishReplay
), but you probably wanna dig into how rxjs works.Nevertheless, this doesn’t have anything to do with Angular itself.
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.