Exploring the contrast between .toPromise().then()
async approach and pipe
async approach
Currently, I am utilizing the toPromise().then()
method for making a synchronous
call
deleteErrorList(errordetails) {
return this.http.post(this.apiUrl + 'RemoveErrorList', errordetails, this.requestOptions)
.toPromise().then((res: Response) => {
return res.json();
})
.catch(error => {
return Observable.throw(error);
});
}
HTML -
*ngFor="let variantLabel of elementData.elementDataCollection"
However, a colleague suggested using map()
and asyncpipe
pattern for handling observables
instead of toPromise()
.
Consequently, I have updated my code to include .map()
instead of toPromise()
like so
deleteErrorList(errordetails) {
return this.http.post(this.apiUrl + 'RemoveErrorList', errordetails, this.requestOptions)
.map((res: Response) => {
return res.json();
})
.catch(error => {
return Observable.throw(error);
});
}
HTML -
*ngFor="let variantLabel of elementData.elementDataCollection | async"
It seems like the code functions in a similar manner for
sync
calls. But what exactly sets them apart? Does it solely serve as a means to handleObservable
? And which approach is preferable?