Reputation: 11
I get a GET request and I would like to wait for a response to run code how do i do this?
My code
valid()
{
let headers = new Headers({ 'Content-Type': 'application/json'});
let options = new RequestOptions({ headers: headers, method: "get" });
return this.http.get(this.urlstring+ "getisvalid" , options ).toPromise().then(response => response.json());
}
//Code that is in the function
this._service.valid().then(resut => this.resut = resut);
Upvotes: 1
Views: 1640
Reputation: 2676
When you subscribe to an observable, you can provide a callback function; in the example below, I call it CompleteValidCall
. CompleteValidCall()
will only be invoked on a successful get that returns data and not an error. You place whatever follow on logic you need in the callback function.
this._service.valid()
.subscribe(
result => this.result = result,
error => this.error = error,
() => this.CompleteValidCall()
);
completeValidCall() {
// the rest of your logic here - only executes on obtaining result.
}
Upvotes: 1
Reputation: 4920
this.http.get return Observable, so you have to subscribe to it. if you change valid() to:
valid() {
let headers = new Headers({ 'Content-Type': 'application/json'});
let options = new RequestOptions({ headers: headers, method: "get"});
return this.http.get(this.urlstring+ "getisvalid" , options ).map(response => response.json());
}
then use:
this._service.valid().subscribe(resut => this.resut = resut);
Upvotes: 0