For educational purposes, I am looking to simulate a lengthy response from the server in Angular 2. Here is the code snippet:
getTasks() : Observable<Task[]> {
return this.http.get('data/tasks.json').map(response => {
return response.json();
});
}
// implementing
getTasks().subscribe(groups => {
this.tasks = groups;
});
I require the server response to take at least 1 second. In Angular 1.5, I achieved this like so:
this.getTasks = function () {
return $http.get("data/tasks.json").then(function (response) {
return $timeout(function () {
return response.data;
}, 1000);
};
}
In Angular 2, following suggestions from this post, I attempted:
getTasks() : Observable<Task[]> {
return this.http.get('data/tasks.json').map(response => {
return Observable.of(response.json()).delay(1000);
});
}
and
getTasks() : Observable<Task[]> {
return this.http.get('data/tasks.json').map(response => {
return Observable.create((obs: Observable<Task[]>) => {
obs.next(response.json());
obs.complete();
).delay(1000);
});
}
However, I am encountering TypeScript compilation errors or receiving an Observable instead of a task collection when using getTasks().subscribe
. It seems like I might be overlooking something. Any assistance would be greatly appreciated. Thank you.