Weird behavior when using curly brackets in arrow function expressions with Fetch API

Viewed 15

I am using the fetch API. I got stuck trying to print the result of the response as I kept getting undefined. Then I copy and pasted the first example in the mozilla docs on fetch and it worked. I played around with the code and noticed that the reason I was getting undefined is because I had curly brackets around the content of the arrow function expression.

fetch(...url)
.then((response) => {response.json()})
.then((data) => {console.log(data)})
// console.log() prints undefined

fetch(...url)
.then((response) => response.json())
.then((data) => console.log(data))
// console.log() prints the expected json output

Why does adding curly brackets result in console.log() outputting undefined in the first snippet?

0 Answers
Related