I found a disturbing fact in my Angular 5 App.
This little call in my service:
interface U{
name:string;
}
...
constructor(private http : *Http*, private httpC:HttpClient) // Http is deprecated - its a compare test
...
ping() //Test Debug func
{
let o:Observable<U> = this.httpC.get<U>(this.url + "user/1");
o.subscribe(resp => {
console.log("SUBSCRIBE1: Init:get:user:U: " + resp.name);
return resp;
},
this.handleError);
o.subscribe(resp => {
console.log("SUBSCRIBE2: Init:get:user:U: " + resp.name);
return resp;
},
this.handleError);
o.toPromise().then(resp => {
console.log("PROMISE: Init:get:user:U: " + resp.name);
return resp;
});
}
Now on ping() call Angular/Browser is actually calling three times "url/user/1" Once for each listener (2 subscribes, 1 promise)
Is that used to be like this?
My understanding of an Observer is that I can add multiple "Listeners" which are waiting for an action. NOT trigger the action.
Whats wrong here? Do I have some broken setup after update or something?
Some clearence or an example how to do something like this right would be realy great! Thanks for any input.
EDIT: I extend the experiment with a further call with deprecated HTTP client which is doing the same. So it seems to be by design. Just dont understand how to catch multiple times WITHOUT calling multiple times.
ping() //Test Debug func
{
let o:Observable<U> = this.httpC.get<U>(this.url + "user/1");
o.subscribe(resp => {
console.log("SUBSCRIBE1: Init:get:user:U: " + resp.name);
console.log(resp);
//this.currentUser = resp;
return resp;
},
this.handleError);
o.subscribe(resp => {
console.log("SUBSCRIBE2: Init:get:user:U: " + resp.name);
console.log(resp);
//this.currentUser = resp;
return resp;
},
this.handleError);
o.toPromise().then(resp => {
console.log("PROMISE1: Init:get:user:U: " + resp.name);
console.log(resp);
//this.currentUser = resp;
return resp;
});
o.toPromise().then(resp => {
console.log("PROMISE2: Init:get:user:U: " + resp.name);
console.log(resp);
//this.currentUser = resp;
return resp;
});
let call:string = this.url + "user/1";
return this.http.get(call)
.toPromise()
.then(resp => {
console.log("OLD HTTP PROMISE 1 " + resp);
return resp;
})
.catch(this.handleError)
.then(resp => {
console.log("OLD HTTP PROMISE 2 " + resp);
return resp;
});
}
And this is what Chrome-Network shows after one ping call:
