Morgan Allen Morgan Allen - 23 days ago 6
Javascript Question

Syntax of fat arrow functions (=>), to use or not to use {} around the body

I am looking at this code - https://facebook.github.io/react-native/docs/network.html

return fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseJson) => {
return responseJson.movies;
})


From what I understand
.then((response) => response.json())
translates into:

.then(function(response) {
return response.json()
}


but I can't figure out what does this translate into? there is an extra
{}
in it

.then((responseJson) => {
return responseJson.movies;
})

Answer

If you don't wrap the body of an arrow function with curly brackets, it will evaluate the expression and return the result implicitly. If you wrap it with curly brackets, the result is not implicitly returned and you have to do it explicitly.

For this reason, the second part 'equals' the following:

.then(function(responseJson) {
    return responseJson.movies;
})