angular 真的符合提取规范吗?

Is angular really following the fetch's specifications?

Angular 的 http 文档说 http 服务的响应 return 遵循 fetch 规范。

https://angular.io/guide/http#parse-to-json

在他们的示例中,这是您可以找到的代码

private extractData(res: Response) {
  let body = res.json();
  return body.data || { };
}

其中,显然,res.json() 的结果 不是承诺

但在 fetch 规范中,response.json() 方法应该 return 一个 Promise.

https://fetch.spec.whatwg.org/#response-class

我是否遗漏了 fetch 规范中的某些内容,或者 Angular 它的实现有误?

查看 angular 的 http 源代码,很明显它 return 不是 Promise:

 json(): any {
    if (typeof this._body === 'string') {
      return JSON.parse(<string>this._body);
    }

    if (this._body instanceof ArrayBuffer) {
      return JSON.parse(this.text());
    }

    return this._body;
  }

  // source https://github.com/angular/angular/blob/master/packages/http/src/body.ts#L26

但是规范说

[NoInterfaceObject, Exposed=(Window,Worker)]
interface Body {
  readonly attribute ReadableStream? body;
  readonly attribute boolean bodyUsed;
  [NewObject] Promise<ArrayBuffer> arrayBuffer();
  [NewObject] Promise<Blob> blob();
  [NewObject] Promise<FormData> formData();
  [NewObject] Promise<any> json();
  [NewObject] Promise<USVString> text();
};

看来 angular 决定不严格遵守规范。

我认为你是对的(因为 consume body algo), they aren't following the spec for fetch, internally they are using JSON.parse to parse the JSON (see: github)。

我认为他们这样做是为了更容易地将错误传播到调用堆栈中,因此用户可以通过 http.get().catch() 轻松地自己捕获它。但要了解更多信息,请尝试通过 gitter.

直接询问他们