I have an angular 2 service that has a function which returns data for a dropdown list. The function returns a promise.
Code from service below:
getStuff(): Promise<Stuff>
{
return this.http.get("http://myserver/myServices/myService.svc/rest/getStuff")
.map(res => this.dataHandler.extractData(res)).toPromise()
.catch(err => this.dataHandler.handleHttpError(err));
}
I then call this function from a save event to update the new saved data so the dropdown which uses this data will have up-to-date data.
My code calling this service:
ngOnInit()
{
this.onSavedSubscription = this.OnSaved.subscribe((data: any) =>
{
if (data.IsSuccessful)
{
this.myService.getStuff().then(
res=>
{
this.myService.stuffs = res;
}
);
}
}
When the above code is called, the getStuff function is called but before it can return updated data from the database, the code execution shifts to "then" and fills this.myService.stuffs with old data that is contained in res. I'm not sure what is going on. I thought "then" wouldn't execute until getStuff finished executing and returned the new data. Please help.