When I use the http get method in Angular, it returns an observable. To test this, I created a local server that serves up a 50 MB JSON file containing all employee data.
import { Injectable } from "@angular/core";
import { Http } from "@angular/http";
import 'rxjs/add/operator/map';
@Injectable()
export class DataServiceService {
constructor(private _http: Http) {}
getEmployeeData() {
return this._http
.get("http://localhost:3000/employees")
.map(response => response.json());
}
}
App.Component.html
{{employeeData$ | async}}
Observable is said to be a stream of data that can change over time. I expected the data to start showing as soon as it begins streaming the employees, but in this case, my page remains blank for 30 seconds before all the employee data suddenly appears.
So, what's the advantage of using an observable? Couldn't I just load all the data into a local array and then display it on the HTML?
Is there a way to make the data display as soon as it receives the first employee, instead of making the user wait for 30 seconds?
I realize that in a real application, I should only fetch the number of employees that can be seen in the browser's view, and then load the next set of employees.