How to create a promise in JavaScript

Creating custom promises enables you to wrap asynchronous operations like timers, callbacks, and legacy APIs in modern promise-based workflows. With over 25 years of experience in software development and as the creator of CoreUI, I’ve built numerous custom promises for complex asynchronous operations. From my expertise, the most effective approach is using the Promise constructor with resolve and reject functions to control the promise state. This pattern allows seamless integration of any asynchronous operation into promise chains.

Use the Promise constructor with resolve and reject functions to create custom asynchronous operations.

const myPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    const success = Math.random() > 0.5
    if (success) {
      resolve('Operation completed successfully')
    } else {
      reject(new Error('Operation failed'))
    }
  }, 1000)
})

Here new Promise((resolve, reject) => {...}) creates a promise with an executor function that receives resolve and reject callbacks. The resolve(value) function fulfills the promise with a value, while reject(error) rejects it with an error. The executor runs immediately when the promise is created, and the promise state transitions based on which callback is called first.

Best Practice Note:

This is the same approach we use in CoreUI components for wrapping animations, API calls, and complex asynchronous workflows. Always call either resolve or reject exactly once - subsequent calls are ignored but it’s good practice to ensure clean promise resolution.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author