🟑 MediumAsync BugsπŸ› Debug Challenge

Promise constructor anti-pattern

Buggy Code β€” Can you spot the issue?

function loadData(url) {
  return new Promise((resolve, reject) => {
    fetch(url)
      .then(r => r.json())
      .then(data => resolve(data))
      .catch(err => reject(err));
  });
}

Fixed Code

function loadData(url) {
  return fetch(url).then(r => r.json());
}

// Or with async/await:
async function loadData(url) {
  const r = await fetch(url);
  return r.json();
}

Bug Explained

Bug: fetch() already returns a Promise. Wrapping it in new Promise is the "explicit Promise constructor anti-pattern" β€” verbose and error-prone.

Explanation: If you're already working with Promises, just return/chain them directly. The new Promise constructor is only needed when working with callbacks.

Key Insight: Never wrap a Promise in new Promise. This is called the explicit constructor anti-pattern.

More Async Bugs Debug Challenges

🟒 EasyMissing await causes wrong resultβ†’πŸŸ‘ MediumSequential awaits killing performanceβ†’πŸŸ‘ MediumSwallowed error in async functionβ†’πŸ”΄ HardAsync function in forEachβ†’

Practice spotting bugs live β†’

38 debug challenges with AI hints

πŸ› Try Debug Lab