The Angular's http
docs says that the response returned by the http
service follows the fetch
specification.
https://angular.io/guide/http#parse-to-json
And in their example, this is the code you can find
private extractData(res: Response) {
let body = res.json();
return body.data || { };
}
In which, obviously, the result of res.json()
is not a promise.
But in the fetch
specification, the response.json()
method is supposed to return a Promise
.
https://fetch.spec.whatwg.org/#response-class
Am I missing something in the fetch
spec or is Angular wrong about it's implementation?
Looking into angular's http source it's clear it doesn't return a Promise:
But the spec says
So it seems that angular decided not to follow the spec strictly.
I think you're right (because of consume body algo), they aren't following the spec for
fetch
, internally they are usingJSON.parse
to parse the JSON (see: github).I think they're doing it to propagate the error more easily up the callstack, so the user can easily catch it himself via
http.get().catch()
. But for more information try to ask them directly via gitter.