Skip to main content

Command Palette

Search for a command to run...

JavaScript Promises Explained Through a Secret Mission With Your Girlfriend

Updated
4 min readView as Markdown
JavaScript Promises Explained Through a Secret Mission With Your Girlfriend
D
CS student focused on backend & OS fundamentals. Building real projects and sharing practical learning.

It’s 2:13 AM.

You and your girlfriend are sneaking into your college campus to retrieve a notebook you “forgot” in the lab.

Security guards are patrolling.

Cameras are rotating.

Your phone is on 12%.

This is not synchronous life.

This is async.

And this is where JavaScript Promises live.


What Is a Promise?

A Promise is basically:

“I will complete this mission… but I don’t know yet if I’ll succeed or fail.”

In JavaScript:

const mission = new Promise((resolve, reject) => {
  let guardSleeping = true;

  setTimeout(() => {
    if (guardSleeping) {
      resolve("Notebook retrieved successfully 📓");
    } else {
      reject("Guard caught you 🚨");
    }
  }, 3000);
});

There are only three states:

  • Pending → You’re inside the campus. Outcome unknown.

  • Fulfilled → You escaped with the notebook.

  • Rejected → You’re explaining yourself to the dean tomorrow.

That’s it.

A Promise is controlled uncertainty.


.then() — If the Mission Succeeds

mission.then((result) => {
  console.log("Success:", result);
});

Translation:

“If we survive, celebrate.”

.then() only runs if the Promise resolves.

No success?

No celebration.


.catch() — If Everything Goes Wrong

mission
  .then((result) => console.log(result))
  .catch((error) => console.log("Damage control:", error));

.catch() is backup planning.

If you don’t handle errors,

your program crashes.

If you don’t handle risk,

your life crashes.

Same logic.


Now It Gets Serious

You split the mission into tasks:

  1. Girlfriend disables camera.

  2. You distract the guard.

  3. Friend waits outside with bike.

Now you have multiple promises running at the same time.


Promise.all() — Everyone Must Do Their Job

Promise.all([disableCamera, distractGuard, startBike])
  .then((results) => {
    console.log("Mission success:", results);
  })
  .catch((error) => {
    console.log("Mission failed because one task failed:", error);
  });

If even ONE person fails…

Mission over.

That’s Promise.all().

Use it when:

  • Every API must succeed.

  • Every file must load.

  • Every task is critical.

One weak link?

Total failure.


Promise.allSettled() — Just Tell Me What Happened

Let’s say you just want the report:

  • Did camera get disabled?

  • Did guard get distracted?

  • Did bike start?

Promise.allSettled([disableCamera, distractGuard, startBike])
  .then((results) => {
    console.log(results);
  });

Even if two fail and one succeeds,

you still get the full status.

This is realistic mode.

In real systems, partial success is common.


Promise.race() — Whoever Finishes First Decides Everything

Imagine you set a timer:

If camera isn’t disabled in 5 seconds,

abort mission.

Promise.race([disableCamera, timeout])
  .then((result) => {
    console.log("First result:", result);
  })
  .catch((error) => {
    console.log("First failure:", error);
  });

Whichever happens first —

success or failure —

ends the race.

Fastest wins.

Even if it’s bad news.


Promise.any() — First Successful Action Wins

Now you try three escape routes:

  • Back gate

  • Front gate

  • Jump wall

You only care about the first one that works.

Promise.any([backGate, frontGate, jumpWall])
  .then((success) => {
    console.log("Escaped through:", success);
  })
  .catch(() => {
    console.log("All escape routes blocked.");
  });

Important difference:

  • race() → first result (even failure)

  • any() → first success only

If all escape routes fail,

then it rejects.

This is smart redundancy.


The Deep Part Beginners Miss

Promises are not about syntax.

They are about managing uncertainty without freezing your entire program.

Without Promises:

Your app would stop and wait.

With Promises:

Your app keeps running while waiting.

Just like in the mission —

you don’t stand still staring at the camera.

You keep moving.