Maybe some pictures help?
The numbers here indicate the order of operations.
- Component is initialized and calls the getMovies method of the movieService.
- The movieService getMovies method returns an Observable. NOT the data at this point.
- The component calls
subscribe
on the returned Observable. - The
get
request is submitted to the server for processing. - The
ngOnInit
method is complete.
Any code here after the subscribe
cannot access the movies
property since the data has not yet been returned.
At some LATER point in time …
- The movies are returned to the service.
- If the process was successful, the first callback function is executed.
- The local movies property is assigned to the movies returned from the service. It is only here that the movies property is finally set.
Attempting to access the movies property prior to step #8 results in an error.