Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Promise executor resolve and reject functions

The Promise constructor supplies two functions to its executor. The first call to either locks in the promise’s outcome, but it does not always settle the promise immediately.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

new Promise((resolve, reject) => { /* Call resolve(value) or reject(reason). */ });

new Promise(executor) requires a function and calls it synchronously with resolve first and reject second. These are conventional parameter names; each pair belongs to one newly created promise. Both functions return undefined and can be saved for later, for example in a callback. Unlike these functions, the static methods Promise.resolve() and Promise.reject() return promises.

resolve(value) immediately fulfills the promise with a primitive value. Passing the promise itself to its own resolve rejects it with a TypeError. For any other object, JavaScript reads its then property synchronously. If the retrieved value is not callable, the promise is fulfilled immediately with the object itself, even if that object is another promise. If the retrieved value is callable, the object is a thenable: JavaScript calls then later, and the promise follows the thenable’s outcome and may remain pending. A throw while reading then, or from calling it before either of its supplied callbacks has been called, also causes rejection.

reject(reason) immediately rejects the promise with the given reason, without following it even if it is a promise. Any value is allowed, but an Error is usually more useful. An omitted argument means undefined. Only the first call to either function in the pair counts, even if the promise is still pending. The outcome is locked in (resolved), but the promise is settled only once it is fulfilled or rejected. Handlers registered with then() and catch() run asynchronously.

The constructor ignores the executor’s return value. A throw that escapes the executor synchronously rejects the promise only if neither function has been called. If neither is ever called and the executor does not throw, the promise stays pending. Avoid an async executor: the constructor ignores the promise it returns, so rejection of that returned promise does not reject the outer promise.

Examples

{
  let finish;
  const inner = new Promise((resolve) => {
    finish = resolve;
  });

  const outer = new Promise((resolve, reject) => {
    console.log("executor");
    resolve(inner);
    reject(new Error("ignored"));
  });

  console.log("after constructor");
  outer.then((value) => {
    console.log(`fulfilled: ${value}`);
    return new Promise((resolve, reject) => {
      reject(new Error("failed"));
    });
  }).catch((reason) => {
    console.log(`rejected: ${reason.message}`);
  });
  finish(42);
}

Result

executor
after constructor
fulfilled: 42
rejected: failed

`executor` appears before `after constructor` because the executor runs synchronously. `resolve(inner)` locks in `outer` while `inner` is still pending, so `reject(new Error("ignored"))` has no effect. `finish(42)` fulfills `inner`; the handler logs `fulfilled: 42`, and `catch()` then handles a separate promise rejected with the message `failed`.

Related lessons

  • JavaScript · Lesson 14 · Intermediate · Version: 3
  • JavaScript · Lesson 15 · Intermediate · Version: 3

Sources