JavaScript new Promise

Beginner
6 min read
Updated: Nov 2026

Definition and Usage

new Promise((resolve, reject) => { ... }) runs the executor immediately. Call resolve(value) to fulfill or reject(error) to reject. Prefer async functions or existing promise-returning APIs when you can.

JavaScript Syntax

JavaScript
const p = new Promise((resolve, reject) => {
  resolve("ok");
});
p.then((v) => console.log(v));
Try it Editor

How It Works

  1. executor. Runs sync when the Promise is created.
  2. resolve. Fulfills with "ok".
  3. then. Reads the value.

Examples

Example 1 — reject

Failure path.

JavaScript
new Promise((_, reject) => reject(new Error("no")))
  .catch((e) => console.log(e.message));
Try it Editor

How It Works

  1. reject with an Error.
  2. catch handles it.

Example 2 — Wrapping callbacks

Classic use case.

JavaScript
function wait(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}
console.log(typeof wait(0).then);
Try it Editor

How It Works

  1. Return a new Promise around setTimeout.

Frequently Asked Questions

When wrapping callback APIs. If you already have a promise, return it—don't wrap again.
It starts synchronously. Only the resolution may happen later.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful