简体   繁体   English

JavaScript提取。 如何处理结果

[英]Javascript fetch. How to handle result

Im currently playing around with fetch and was wondering how i can make the callbacks a bit cleaner but still use arrow function. 我目前正在玩提取,并且想知道如何使回调更清晰但仍使用箭头功能。 For example i want to make a loop on the success. 例如,我想对成功进行循环。 What syntax should i use? 我应该使用什么语法?

Code

fetch(url).then(res => res.json())
  .then(data => console.log(data.items))
  .catch(e => console.log('error'))

There's not much else to do than use a couple of curly brackets and iterate in the usual way 除了使用几个大括号并以通常的方式进行迭代外,没有其他事情要做

fetch(url).then(res => res.json())
          .then(data => {
             for (const item of data.items) {
                // stuff
             }
          })
          .catch(e => console.log('error'))

You could of course put that in a function and call the function instead, if that makes you happier. 当然,您可以将其放在函数中,然后调用该函数,如果这样会使您更快乐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM