Understanding Observables can be a bit tricky for me at times, leading to some confusion.
Let's say we are subscribing to getData in order to retrieve JSON data asynchronously:
this.getData(id)
.subscribe(res => {
console.log(data.items[0])
// more data processing
})
This approach works, but it doesn't seem very elegant to process the response data within the .subscribe block. Storing the response in a variable seems like a better option:
let data;
this.getData(id)
.subscribe(res => data = res)
console.log(data.items[0])
// more data processing
However, when trying this method, an error occurs due to 'data' not having an initial type.
TypeError: Cannot read property 'items' of undefined
Creating an interface for the JSON response might seem unnecessary. What am I missing?
Furthermore, setting up a callback function appears redundant as it requires using two functions to achieve what should ideally be done with just one.