Javascript
May 4, 2026
Spot the bug: This async function should fetch and return data.
async function getData(url) {
fetch(url)
.then(res => res.json())
.then(data => {
return data
})
}
Explanation
This is a very common mistake. When you use return inside a .then() callback, it returns the value to the .then() chain — not to the outer async function. So getData() returns undefined. Fix it by using await: const data = await fetch(url).then(r => r.json()); return data.
📝
Reviewed by CodeShot Editorial
Every challenge is code-reviewed by senior developers to ensure accuracy and real-world relevance. Learn more.