Course contentsShow
← JavaScript
Lesson 1273 of 2,72329. PromisesFree lesson

Promise Constructor Anti-Pattern

Avoid wrapping already-promise-based code in new Promise; this creates unnecessary nesting.

Promise Constructor Anti-Pattern

You'll learn why wrapping code that already returns a promise in new Promise() creates unnecessary complexity and how to avoid it.

What is the Anti-Pattern?

The Promise Constructor Anti-Pattern happens when you wrap an operation that already returns a promise inside a new Promise() constructor. This creates an extra, unnecessary layer of promise wrapping that makes your code harder to read and maintain.

Think of it like double-bagging groceries when they're already in a perfectly good bag — you're adding extra work and material for no benefit.

The Problem in Practice

Since you've learned how to create promises with the constructor, you might be tempted to always wrap async operations this way:

// ❌ ANTI-PATTERN: Don't do this!
function getUser(id) {
  return new Promise((resolve, reject) => {
    // someAsyncFunction already returns a promise
    someAsyncFunction(id)
      .then(result => resolve(result))
      .catch(error => reject(error));
  });
}

The issue? someAsyncFunction(id) already gives you a promise. You're creating a second promise just to forward the result of the first one.

The Simple Solution

Just return the promise directly:

// ✅ BETTER: Return the existing promise
function getUser(id) {
  return someAsyncFunction(id);
}

This is cleaner, more readable, and avoids unnecessary nesting.

When to Use the Constructor

The new Promise() constructor is meant for wrapping non-promise-based async operations (like callbacks or timers), not for wrapping promises themselves.

Key Takeaway: If an operation already returns a promise, return it directly — don't wrap it in new Promise(). Use the constructor only when converting non-promise-based code.