[英]What is the return type of the json method of a response?
I see some examples of Angular 2, which convert the response of http
into JavaScript object like this: 我看到一些Angular 2的例子,它们将
http
的响应转换为JavaScript对象,如下所示:
http.get("http://....").subscribe(
response => {
let result = response.json();
},
error => {
console.log('cannot get api');
}
);
I also see some examples which state that json()
method returns a Promise, which requires the function to be enclosed in then
to obtain the result: 我还看到一些例子,其中指出
json()
方法返回一个无极,这需要该函数被封闭在then
,得到的结果:
response.json().then(result => ({
///
});
Why does one use then
and the other does not? 为什么一个使用
then
另一个不使用? What's the return of value of json
and which should I use? 什么是
json
的价值回报,我应该使用哪些?
The examples where you see response.json()
returning a Promise
are part of the Fetch API, which is still experimental: https://developer.mozilla.org/en-US/docs/Web/API/Response 您看到
response.json()
返回Promise
的示例是Fetch API的一部分,它仍然是实验性的: https : //developer.mozilla.org/en-US/docs/Web/API/Response
Body.json()
Body.json()
Takes a Response stream and reads it to completion.
获取响应流并将其读取完成。 It returns a promise that resolves with a JSON object.
它返回一个使用JSON对象解析的promise。
The other example you see are from Angular's implementation which returns the data directly instead of a Promise
: https://github.com/angular/angular/blob/master/packages/http/src/body.ts#L26 您看到的另一个示例来自Angular的实现,它直接返回数据而不是
Promise
: https : //github.com/angular/angular/blob/master/packages/http/src/body.ts#L26
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.