Facebook Pixel

How to Create a Promise in JavaScript

Creating promises with new Promise, the executor, and Promise.resolve/reject. Here is the complete guide.

How to Create a Promise in JavaScript

There are three main ways to create a promise. Here is each one with examples.

1. new Promise (For Async Operations)

const promise = new Promise((resolve, reject) => { setTimeout(() => { const success = true; if (success) resolve("data"); else reject(new Error("failed")); }, 1000); }); `` The executor `(resolve, reject) => {}` runs immediately. Call `resolve(value)` to fulfill, `reject(reason)` to reject. ### 2. Promise.resolve (Already Fulfilled) ```js const p = Promise.resolve(42); p.then((v) => console.log(v)); // 42 `` ### 3. Promise.reject (Already Rejected) ```js const p = Promise.reject(new Error("oops")); p.catch((err) => console.error(err)); `` ### The Executor ```js const p = new Promise((resolve, reject) => { // runs synchronously, immediately // start async work // call resolve(value) or reject(reason) when done }); `` - Runs immediately (synchronously). - `resolve`/`reject` can be called async (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. ### Wrapping a Callback-Based API ```js function readFileP(path) { return new Promise((resolve, reject) => { fs.readFile(path, "utf8", (err, data) => { if (err) reject(err); else resolve(data); }); }); } `` This is how you promisify a callback-based function manually. ### Resolving With a Promise ```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 outer promise adopts the inner promise's state. ### 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. Wrap callback-based APIs by calling `resolve`/`reject` inside the callback. Resolving with a promise makes the outer 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.).

Return new Promise((resolve, reject) => { callbackApi(args, (err, result) => { if (err) reject(err); else resolve(result); }); }). The callback follows the error-first convention.

The promise is rejected with the thrown error. The Promise constructor catches thrown errors automatically. You do not need to try/catch inside the executor.

The outer promise adopts the inner promise's state. If the inner 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.

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.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.