javascript react-native fetch 中的“then(res => res.json())”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46579533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:47:26  来源:igfitidea点击:

What does 'then(res => res.json())' in react-native fetch mean?

javascriptreact-nativejsxfetch-api

提问by Aniket Singh

What is meant by then(res => res.json())in the snippet below in react-native fetch?

then(res => res.json())以下代码段中 react-native fetch是什么意思?

fetch(url)
      .then(res => res.json())
      .then(res => {
        this.setState({
          data: res,
          error: res.error || null,
          loading: false
        });

采纳答案by GottZ

That's not really a react question since fetch and then are parts of js itself.

这不是一个真正的反应问题,因为 fetch 然后是 js 本身的一部分。

fetch returns an object as Promise that contains various information like headers, HTTP status etc. etc.

fetch 返回一个对象作为 Promise,其中包含各种信息,如标头、HTTP 状态等。

You have res.json()and various other possibilities. .json()will just return the body as promise with json content.

你有res.json()和其他各种可能性。.json()只会将主体作为带有 json 内容的承诺返回。

For more info: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

更多信息:https: //developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

You can return the data as following:

您可以按如下方式返回数据:

  • .arrayBuffer()
  • .blob()
  • .json()
  • .text()
  • .formData()
  • .arrayBuffer()
  • .blob()
  • .json()
  • .text()
  • .formData()

回答by Duc Filan

Your code part:

您的代码部分:

res => res.json()

is an ES6 arrow function, which is translated to:

是一个ES6 箭头函数,翻译为:

function(res){
    return res.json();
}

And, about the json()function:

而且,关于json()功能:

The json()method of the Body mixin takes a Response stream and reads it to completion. It returns a promise that resolves with the result of parsing the body text as JSON.

json()Body mixin的方法接受一个 Response 流并读取它完成。它返回一个承诺,该承诺将正文文本解析为 JSON 的结果进行解析。

Read more here.

在这里阅读更多。

回答by sabithpocker

Javascript fetchfunction asynchronously pulls a resource from the specified url. Meanwhile fetchreturns a Promise. Promisehelps with the asynchronous part and runs the function passed into then(res => res.json()) once the resource is loaded with the fetched resource as parameter. The fetched resource can be parsed using json()if it is JSON formatted.

Javascriptfetch函数从指定的url. 同时fetch返回一个Promise. Promise帮助处理异步部分并运行传入then( res => res.json())的函数,一旦以获取的资源作为参数加载资源。如果获取的资源json()是 JSON 格式,则可以使用它进行解析。

thenalso returns a Promisemaking it chainable.

then还返回一个Promise使其可链接。

fetch(url) // asynchronously load contents of the url
           // return a Promise that resolves when res is loaded
      .then(res => res.json()) // call this function when res is loaded
      // return a Promise with result of above function
      .then(res => { // call this function when the above chained Promise resolves
        this.setState({
          data: res,
          error: res.error || null,
          loading: false
        });

res => res.json()can also be written as (but not exactly equal)

res => res.json()也可以写成(但不完全相等

function(res) { return res.json()}