🟑 MediumAsync BugsπŸ› Debug Challenge

Swallowed error in async function

Buggy Code β€” Can you spot the issue?

async function saveData(data) {
  try {
    const result = await api.post('/save', data);
    return result;
  } catch (err) {
    console.log('Error:', err);
  }
}

// Caller
const result = await saveData(data);
if (result) updateUI(result); // silently fails on error

Fixed Code

async function saveData(data) {
  try {
    const result = await api.post('/save', data);
    return result;
  } catch (err) {
    console.error('Error saving data:', err);
    throw err; // re-throw so caller can handle it
  }
}

// Caller
try {
  const result = await saveData(data);
  updateUI(result);
} catch (err) {
  showErrorMessage(err);
}

Bug Explained

Bug: The catch block logs but doesn't re-throw. The function returns undefined on error. Caller can't distinguish success from failure.

Explanation: Catching an error and not re-throwing it silently swallows it. The function returns undefined and callers can't tell what happened. Always re-throw or return an error indicator.

Key Insight: If you catch an error and don't re-throw it, callers are flying blind. Only catch if you can truly handle it.

More Async Bugs Debug Challenges

🟒 EasyMissing await causes wrong resultβ†’πŸŸ‘ MediumSequential awaits killing performanceβ†’πŸ”΄ HardAsync function in forEachβ†’πŸ”΄ HardRace condition in state updateβ†’

Practice spotting bugs live β†’

38 debug challenges with AI hints

πŸ› Try Debug Lab