0

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'))

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This was exactly what i looked for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.