在react-native fetch中 then res res.json 是什么意思

以下片段中的then(res => res.json())是什么意思在react-native fetch中?在react-native fetch中,'then(res => res.json())'是什么意思?

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

您的代码部分:

res => res.json() 

ES6 arrow function,其被翻译成:

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

而且,关于json()功能:

json()方法正文mixin需要响应流和 将其读取完成。它返回一个承诺,将解析正文文本的结果作为JSON解析为 。

了解更多here

Javascript fetch函数异步地从指定的url中提取资源。同时fetch返回Promise。 Promise可以帮助执行异步部分,并在资源以获取的资源作为参数加载后运行传入thenres => res.json())的函数。如果获取的资源是JSON格式,则可以使用json()进行解析。

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()也可以写为(but not exactly equal

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值