4rpit 4rpit - 27 days ago 6
Javascript Question

Angular2: Synching two post requests

I have two ajax post request which need to be executed as synchronous calls.
Scenario is that the form is creating two differnt type of objects, these objects need to be sent to the different API as post request and I have to show animation on the UI until both API returns result to UI.

Any Help!

Edit to add Code

doApproveSingle() {
//animation part
this.loading = true
//call 1
this._http.post('api/single', this.approvedChanges, contentHeaders).subscribe(resp => {
}, error => {
})
//call 2
this._http.post('api/new', this.approveNew, contentHeaders).subscribe(resp => {
}, error => {
})
}


there are two post requests I need to close that animation after both calls are completed, need help in that part.

Answer
  doApproveSingle() {
    //animation part
    this.loading = true
    //call 1
    let obs1 = this._http.post('api/single', this.approvedChanges, contentHeaders)
    .map(resp => { ... });
    //call 2
    let obs2 = this._http.post('api/new', this.approveNew, contentHeaders)
    .map(resp => { ... });
    Observable.zip([obs1, obs2]).subscribe(val => this.loading = false);
  }

If there is nothing to do when the individual HTTP calls complete, the .map(...) parts can be omitted.