I am currently puzzled by a specific syntax that I have encountered while working with the map function and observables in TypeScript/Angular (Angular 5). Here are two methods I am dealing with:
The first method is within a component:
logout() {
this.authService.logout().subscribe(
result => {
this.router.navigate(['/login']);
}
);
}
And the second method is in the related service:
logout(): Observable<any> {
return this.http.post('/api/auth/logout', { }).map(
response => {
this._token = null;
//more unrelated code...
return true
}
);
}
What confuses me in both of these instances is this segment of code:
thing => {
//code
}
I wonder about the significance of 'thing'. The code seems to work, but I see that 'result' and 'response' are used for this 'thing'. Is 'thing' just a placeholder, or is it defined somewhere else?
Additionally, after researching the map function in JavaScript on w3schools, I noticed that the first parameter is supposed to be a function applied to each element of an associated array. However, "thing => {}" does not appear to be a standard function, adding to my confusion.
It's important to note that I have framed my question to address the underlying misunderstanding rather than solely focusing on my immediate issue. Resolving the problem at hand may shed light on my overall confusion.
Although the code functions correctly, it fails to handle a scenario where the API endpoint returns a 500 error. My goal is to figure out how to catch errors like these to manage them on the front end.