BitlystBitlyst
PostsSeriesTopicsAbout

Bitlyst

Short, diagram-first posts on JavaScript, React & Next.js. Follow via RSS or the newsletter.

ยฉ 2026 Mohsen Fallahnejad

PostsSeriesTopicsLinksAboutRSSGitHubX
Posts/javascript
Beginner2 min read

JavaScript Promises Explained

A simple but deep dive into how Promises work in JavaScript and why they matter.

Mohsen Fallahnejad

Mohsen Fallahnejad

Sep 23, 2025Discuss

๐Ÿš€ What is a Promise?#

  • A Promise is a special JavaScript object that represents the result of an asynchronous operation.
  • Itโ€™s like a placeholder for a value that will be available later (success or failure).

Think: โ€œI promise Iโ€™ll give you the result later.โ€


๐Ÿ“ฆ The 3 States of a Promise#

  1. Pending โ€“ still waiting for the result.
  2. Fulfilled โ€“ completed successfully, has a value.
  3. Rejected โ€“ failed, has a reason (usually an error).

Once a promise is fulfilled or rejected, itโ€™s settled and cannot change again.


๐Ÿ›  How to Create a Promise#

js
const myPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve("Done! โœ…");
    // or reject("Something went wrong โŒ");
  }, 1000);
});
  • The function inside new Promise runs immediately.
  • Call resolve(value) to fulfill.
  • Call reject(error) to fail.

โณ Using a Promise#

With .then and .catch#

js
myPromise
  .then(result => console.log(result))  // "Done! โœ…"
  .catch(err => console.error(err));

With async/await (sugar syntax)#

js
async function run() {
  try {
    const result = await myPromise;
    console.log(result); // "Done! โœ…"
  } catch (err) {
    console.error(err);
  }
}

โšก Chaining Promises#

You can chain .then calls to transform values step by step:

js
fetch("/data.json")
  .then(res => res.json())
  .then(data => data.items)
  .then(items => console.log(items))
  .catch(err => console.error(err));

Each .then returns a new Promise, which makes chaining possible.


๐Ÿ›‘ Common Mistakes#

  • Forgetting to return:
    js
    .then(() => { doSomething(); })   // โŒ returns undefined
    .then(() => doSomething())        // โœ… returns the promise/value
    
  • Mixing callbacks + promises unnecessarily.
  • Not handling errors: Always end with .catch or use try/catch.

๐Ÿ”„ Promise Utilities#

  • Promise.all([p1, p2]) โ†’ waits for all to fulfill, fails fast if one rejects.
  • Promise.race([p1, p2]) โ†’ returns the first settled (fulfilled or rejected).
  • Promise.allSettled([p1, p2]) โ†’ waits for all, returns results with status.
  • Promise.any([p1, p2]) โ†’ first fulfilled, ignores rejections until all reject.

๐Ÿ” How Promises work under the hood#

  • Resolvers (resolve/reject) queue their reactions as microtasks.
  • This means .then/catch callbacks always run after the current synchronous code, but before macrotasks like setTimeout.
js
console.log("Start");

Promise.resolve().then(() => console.log("Microtask"));
setTimeout(() => console.log("Macrotask"), 0);

console.log("End");

Output:

code
Start
End
Microtask
Macrotask

โœ… Summary#

  • A Promise is an object that represents future values.
  • Has 3 states: pending, fulfilled, rejected.
  • Use .then/.catch or async/await to consume.
  • Promises are scheduled as microtasks in the event loop, making them powerful for async flow control.

โœจ Promises are the foundation of async/await and modern JavaScript concurrency.

Filed under#javascript#async#promise#event loop

Was this worth your time?

One tap tells me what to write more of.

Mohsen Fallahnejad

Written by

Mohsen Fallahnejad

Frontend engineer explaining JavaScript, React & Next.js internals in posts you can finish over coffee. More about me

X โ†—GitHub โ†—LinkedIn โ†—
โ† OlderJavaScript is Single-ThreadedNewer โ†’JavaScript Closures โ€” A Simple Deep Dive

Start the discussion

Questions, corrections, a better approach? I read and reply to every comment.

Read next

queueMicrotask Explained: The Smallest Async Queue in JavaScript

What queueMicrotask does, how it differs from Promise.then, and how microtasks really work under the hood.

Dec 31, 20251 min read1

Why async/await Doesnโ€™t Make JavaScript Code Parallel

Async/await feels like parallel code, but it isnโ€™t. Hereโ€™s why JavaScript is still single-threaded and how async really works.

Dec 30, 20253 min read1

How Promise.all Works in a Single-Threaded JavaScript World

If JavaScript is single-threaded, how can Promise.all handle multiple requests at the same time? A deep dive into the event loop and async I/O.

Dec 30, 20252 min read1

Get new posts in your inbox

No spam. Unsubscribe any time.

2 min left
Discuss๐Ÿ‘ React
๐Ÿ‘