棱角是否真的遵循fetch的规格? [英] Is angular really following the fetch's specifications?

查看:159
本文介绍了棱角是否真的遵循fetch的规格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular的 http docs表示 http 服务返回的响应遵循获取规范。

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 || { };
}

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

但是在 fetch 规范, response.json()方法应返回 Promise

But in the fetch specification, the response.json() method is supposed to return a Promise.

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

我错过了 fetch 规范或Angular错误的内容关于它的实现?

Am I missing something in the fetch spec or is Angular wrong about it's implementation?

推荐答案

调查angular的http源很明显它不会返回Promise:

Looking into angular's http source it's clear it doesn't return a 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();
};

所以似乎角度决定不严格遵守规范。

So it seems that angular decided not to follow the spec strictly.

这篇关于棱角是否真的遵循fetch的规格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆