Here will discuss using Promises instead of Observables in Angular.
In Angular, we can use either Promises or Observables. By default, the Angular Http service returns an Observable. To prove this, hover the mouse over the get() method of the Http service in employee.service.ts file. Notice from the IntelliSense, that it returns Observable<Response>
Services
import { Injectable } from '@angular/core';
import { IEmployee } from './employee';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/Observable/throw';
// import toPromise operator
import 'rxjs/add/operator/toPromise';
@Injectable()
export class EmployeeService {
constructor(private _http: Http) { }
// Notice we changed the return type of the method to Promise<IEmployee>
// from Observable<IEmployee>. We are using toPromise() operator to
// return a Promise. When an exception is thrown handlePromiseError()
getEmployeeByCode(empCode: string): Promise<IEmployee> {
return this._http.get("http://localhost:24535/api/employees/" + empCode)
.map((response: Response) => <IEmployee>response.json())
.toPromise()
.catch(this.handlePromiseError);
}
// This method is introduced to handle exceptions
handlePromiseError(error: Response) {
console.error(error);
throw (error);
}
}
Controller
import { Component, OnInit } from '@angular/core';
import { IEmployee } from './employee';
import { EmployeeService } from './employee.service';
@Component({
selector: 'my-employee',
templateUrl: 'app/employee/employee.component.html',
styleUrls: ['app/employee/employee.component.css']
})
export class EmployeeComponent implements OnInit {
employee: IEmployee;
statusMessage :string;
constructor(private _employeeService: EmployeeService,
ngOnInit() {
let empCode: string = this._activatedRoute.snapshot.params['code'];
// The only change that we need to make here is use
// then() method instead of subscribe() method
this._employeeService.getEmployeeByCode(empCode)
.then((employeeData) => {
if (employeeData == null) {
this.statusMessage =
'Employee with the specified Employee Code does not exist';
}
else {
this.employee = employeeData;
}
},
(error) => {
this.statusMessage =
'Problem with the service. Please try again after sometime';
console.error(error);
});
}
}
In Angular, we can use either Promises or Observables. By default, the Angular Http service returns an Observable. To prove this, hover the mouse over the get() method of the Http service in employee.service.ts file. Notice from the IntelliSense, that it returns Observable<Response>
Services
import { Injectable } from '@angular/core';
import { IEmployee } from './employee';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/Observable/throw';
// import toPromise operator
import 'rxjs/add/operator/toPromise';
@Injectable()
export class EmployeeService {
constructor(private _http: Http) { }
// Notice we changed the return type of the method to Promise<IEmployee>
// from Observable<IEmployee>. We are using toPromise() operator to
// return a Promise. When an exception is thrown handlePromiseError()
getEmployeeByCode(empCode: string): Promise<IEmployee> {
return this._http.get("http://localhost:24535/api/employees/" + empCode)
.map((response: Response) => <IEmployee>response.json())
.toPromise()
.catch(this.handlePromiseError);
}
// This method is introduced to handle exceptions
handlePromiseError(error: Response) {
console.error(error);
throw (error);
}
}
Controller
import { Component, OnInit } from '@angular/core';
import { IEmployee } from './employee';
import { EmployeeService } from './employee.service';
@Component({
selector: 'my-employee',
templateUrl: 'app/employee/employee.component.html',
styleUrls: ['app/employee/employee.component.css']
})
export class EmployeeComponent implements OnInit {
employee: IEmployee;
statusMessage :string;
constructor(private _employeeService: EmployeeService,
ngOnInit() {
let empCode: string = this._activatedRoute.snapshot.params['code'];
// The only change that we need to make here is use
// then() method instead of subscribe() method
this._employeeService.getEmployeeByCode(empCode)
.then((employeeData) => {
if (employeeData == null) {
this.statusMessage =
'Employee with the specified Employee Code does not exist';
}
else {
this.employee = employeeData;
}
},
(error) => {
this.statusMessage =
'Problem with the service. Please try again after sometime';
console.error(error);
});
}
}
No comments:
Post a Comment