function loadData(url) {
return new Promise((resolve, reject) => {
fetch(url)
.then(r => r.json())
.then(data => resolve(data))
.catch(err => reject(err));
});
}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: 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.