Creating Promises in JavaScript
How to create promises with new Promise, Promise.resolve, and Promise.reject. Here is each way.
Creating Promises in JavaScript
There are three main ways to create a promise. Here is each one and when to use it.
1. new Promise (For Async Operations)
const promise = new Promise((resolve, reject) => { setTimeout(() => { if (success) resolve("data"); else reject(new Error("failed")); }, 1000); }); `` The executor function `(resolve, reject) => {}` runs immediately. Call `resolve(value)` to fulfill, `reject(reason)` to reject. **Use for**: wrapping callback-based APIs, custom async operations. ### 2. Promise.resolve (Already Fulfilled) ```js const p = Promise.resolve(42); p.then((v) => console.log(v)); // 42 `` Creates a promise that is already fulfilled with the given value. **Use for**: when you have a value but need a promise (e.g., returning from a function that sometimes does sync work). ```js function getUser(id) { if (cache[id]) return Promise.resolve(cache[id]); return fetchUser(id); } `` ### 3. Promise.reject (Already Rejected) ```js const p = Promise.reject(new Error("oops")); p.catch((err) => console.error(err)); `` Creates a promise that is already rejected with the given reason. **Use for**: early validation failures in promise chains. ```js function validate(input) { if (!input) return Promise.reject(new Error("input is required")); return process(input); } `` ### Promise Constructor Details ```js const p = new Promise((resolve, reject) => { // this runs synchronously, immediately // start an async operation // call resolve(value) or reject(reason) when done }); `` - The executor runs **immediately** (synchronously). - `resolve`/`reject` can be called asynchronously (inside a timer, event, etc.). - If the executor throws, the promise is rejected with the thrown error. - Calling `resolve`/`reject` after settling is a no-op. ### Resolving With a Promise If you call `resolve(anotherPromise)`, the outer promise adopts the state of `anotherPromise`: ```js const inner = new Promise((resolve) => setTimeout(() => resolve("inner"), 1000)); const outer = new Promise((resolve) => resolve(inner)); outer.then((v) => console.log(v)); // "inner" after 1 second `` ### The Takeaway Create promises with `new Promise((resolve, reject) => {})` for async operations, `Promise.resolve(value)` for already-fulfilled, and `Promise.reject(reason)` for already-rejected. The executor runs synchronously. `resolve`/`reject` can be called async. Resolving with a promise makes the outer promise adopt its state.
Use new Promise((resolve, reject) => { ... }). The executor runs immediately. Call resolve(value) to fulfill or reject(reason) to reject. For already-settled promises, use Promise.resolve(value) or Promise.reject(reason).
Immediately and synchronously, when the promise is created. The executor is not deferred. But resolve/reject can be called asynchronously (inside a setTimeout, event handler, etc.).
Creating an already-fulfilled promise. Use it when you have a value but need a promise (e.g., returning from a function that sometimes does sync work). Example: if (cached) return Promise.resolve(cached); else return fetch(url);
The outer promise adopts the state of the inner promise. If the inner promise fulfills, the outer fulfills with the same value. If the inner rejects, the outer rejects with the same reason. The outer waits for the inner.
The promise is rejected with the thrown error. You do not need to try/catch inside the executor; the Promise constructor catches thrown errors and rejects automatically.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

