I'm struggling to grasp the concept of RxJS Observables, even though I have utilized the observable pattern in various scenarios in my life. Below is a snippet of code that showcases my confusion:
const observable: Observable<Response> = createdElswhere();
let test1: Observable<string>;
let test2: Observable<string[]>;
test1 = observable.map(r => r.json());
test2 = observable.map(r => r.json());
The Response.json() function returns an array of data - typically objects that are mapped to a TypeScript object, but in this case they are simply strings:
[
"test1",
"test2",
"test3",
"test4"
]
Upon subscribing to either the test1
or test2
properties (whether in an Angular view template or via manual code using .subscribe()
, for instance), it appears that the observable's generic type does not affect how the framework handles them. The output is identical despite the difference in the 'data' within the observables (one being an array and the other an array of observable strings).
Could someone clarify the disparity between Observable<string>
and Observable<string[]>
, providing an illustrative example of their correct usage?