Angular2, typescript
I have a component that displays 1 object at a time from the server.
On the click of a button, the form does some action on the object, sends data to the server. Immediately in the next line, the next object is got.
this.rawS.updateTags({......}); //Update object
this.candidateC.footerNavigateTo(.....);//Get next object
But there is a race condition where ajax to get next object reaches the server before the updateTags ajax reaches the server.
Code of updateTags is
updateTags(rawCandidate) {
this.baseAjaxService.doPost(this.baseUrl + 'tags/update', rawCandidate)
.subscribe(data => {
console.log(data);
});
}
Code for doPost is
doPost(url, inputParamJSON){
let httpResponse = this.http
.post(url, inputParamJSON, this.options)
.map(this.extractData)
.catch(this.handleAjaxError);
return httpResponse;
}
The route for 'continue_screening' has a route resolver which brings the next object.
How can I ensure that footerNavigateTo() runs only after updateTags has completed its full cycle?
this.candidateC.footerNavigateTo()before thethis.rawS.updateTags()callback is invoked. In other words, you're not waiting for the AJAX call to complete before acting on it. @Vincismique's answer is correct, you need to useflatMapin order to chain the calls. This answer also might help: stackoverflow.com/questions/34104638/…