CKTIS / JavaScript
Promise — operation results and resolve/reject functions
Promise represents an eventual result or failure reason. Learn how its executor runs synchronously, how resolve and reject control the outcome, and how then and catch handle that outcome asynchronously.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
new Promise((resolve, reject) => { /* report completion */ });
promise.then(onFulfilled, onRejected);
promise.catch(onRejected);Promise represents the result of an operation that may finish later. Its state is pending, fulfilled, or rejected; the last two are final. Creating a promise does not itself move work onto another thread.
Promise executor resolve and reject functions are supplied when new Promise(executor) calls the executor synchronously. resolve(value) fulfills with an ordinary value or adopts the outcome of a promise or thenable, an object with a callable then method. reject(reason) rejects the promise; prefer an Error as the reason. The first call to either function locks out subsequent calls, even while an adopted outcome remains pending. Resolving therefore does not always mean immediate fulfillment.
The executor's return value is ignored. A synchronous exception thrown inside it rejects the promise unless resolve or reject was already called. Handlers registered through then run asynchronously, after the current execution finishes. then returns a new promise: a returned value determines its outcome, while a thrown exception rejects it. catch handles rejections.
In the example, the executor prints executor, and synchronous code prints sync before result: 42. Calling reject after resolve(21) has no effect. The example requires a runtime with Promise and console; it assumes neither additional Web APIs nor a particular browser version.
Examples
const promise = new Promise((resolve, reject) => {
console.log("executor");
resolve(21);
reject(new Error("ignored"));
});
promise
.then(value => value * 2)
.then(value => console.log(`result: ${value}`))
.catch(error => console.log(error.message));
console.log("sync");Result
executor sync result: 42
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.