This is more like question than resolve a problem.
I would like to know if there any scenario that both "Success" and Error" is not triggered.
The post call to "/logout"
will result Http status return code 200
with empty respond body which is expected
import { httpClient } from angular/common/http;
private http: HttpClient;
this.http.post<any>('/logout', {})
.subscribe(() => {
console.log("Logout");
}, error => {
console.log(error);
},
() => {
console.log("Finally);
});
It wont trigger either "success" call (because no data come back)?, and error not trigger to.
CodePudding user response:
httpClient request is related with http status code.
If you make a request, you will receive a response in any form.
This document is related to the http response status code. Try read the page.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
CodePudding user response:
Code bellow:
this.http
.get('/logout')
.pipe(
map(() => { // OK
return { success: true, err: null };
}),
timeout(10000), // CONTROL TIMEOUT
catchError((e) => { // IN CASE OF ERROR
return of({success: false, err:e});
})
)
.subscribe((result) => { // FINALLY here
if (result.success) {
console.log('Logged out successfully');
} else {
console.log('Logout failed', result.err);
}
});
CodePudding user response:
Observable
, in general, are not required to complete or error. They may remain live, and continue to emit values, forever.
However, Observable
returned by HttpClient
are guaranteed to terminate with either success or error (though the error may take a few minutes in case of a timeout). Whether the observable completes normally or with an error is determined by the HTTP status of the response, the presence of absence of a body does not matter.