Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
173
rated 0 times [  174] [ 1]  / answers: 1 / hits: 25940  / 6 Years ago, sat, september 8, 2018, 12:00:00

Is there any specific technical drawback to using the native Fetch API rather than HttpClient in Angular? I'm relatively new to Angular, and unsure about whether it's safe to 'circumvent' the provided interfaces in such a way. (As an example, it seems directly modifying the DOM by accessing document is not advisable, according to the documentation.)



I'm using Angular 6, and I'm not concerned about clients that don't support fetch.


More From » angular

 Answers
11

Angular is an opinionated framework—meaning the framework wants you to do things the Angular way. That doesn't mean you have to do things their way.



Use either the fetch() or the httpClient freely. They're two different approaches to the same problem and you should pick one based on the context of your needs.



Using fetch() will return a promise. Using Angular's httpClient will return an Observable, which has features that Promises don't. You can convert it with Observable.toPromise(), but then... why use an Observable?



Here's why Angular wants you to use httpClient:




Additional benefits of HttpClient include testability features, typed request and response objects, request and response interception, Observable apis, and streamlined error handling.



[#53540] Tuesday, September 4, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaquelineh

Total Points: 360
Total Questions: 105
Total Answers: 114

Location: Saint Pierre and Miquelon
Member since Fri, Jan 28, 2022
2 Years ago
;