0

The following code works:

.then(jsonResponse => {
            if (!jsonResponse.tracks) {
                return [];
            }
            return jsonResponse.tracks.items.map(track => (
                {
                    id: track.id,
                    name: track.name,
                    artists: track.artists[0],
                    album: track.album.name,
                    uri: track.uri
                }
            ))
})

Why are we using .map(track => (…)) and not .map(track => {…}) as usually with arrow functions?

SirNoob
  • 569
  • 1
  • 4
  • 18
  • 1
    Because it's ambiguous whether you are returning an object or defining a block-scope. When you return an object without a `return` statement, you need to enclose it in parens... – Mr. Polywhirl Nov 02 '20 at 12:42
  • If we don't use `{}`, then arrow function only accepts one expression after `=>`. If we want our expression to expand on more than 1 lines (usually if expression is too long), then we wrap it in `()`. – asimhashmi Nov 02 '20 at 12:44

0 Answers0