This is the HTTP Service, makes a get request that fetches longArray.js;
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/Rx';
@Injectable()
export class HttpService {
constructor(
private http: Http
) {}
getHttpReq(url: string): Promise<any> {
return new Promise((resolve, reject) => {
this.http.get('http://192.168.0.189/longArray.js')
.map(response => response.json())
.subscribe(
function(response) {
console.log("Success Response ");
resolve(response);
},
function(error) {
console.log("Error happened " + error);
reject(error);
},
function() {
console.log("Subscription is completed ");
}
);
});
}
}
This is a method that returns a bool promise depending on if longArray.js was fetched by getHttpReq (in class HttpService) or not;
getLongArrayTryOrFail(): Promise<boolean> {
return new Promise((resolve,reject) => {
this.httpService.getHttpReq('')
.then(response => resolve(true))
.catch(error => reject(false))
});
}
- Is this method of creating HTTP Service correct?
- Basically getHttpReq (in class HttpService) Subscribes to an Observable & either resolves or rejects the promise depending on subscription's successful response or error. Am I correct?
toPromise()on the observable returned byhttp.get. - user663031