Skip to content

Promise

Medium — good to knowFrontend

ELI5 — The Vibe Check

A Promise is JavaScript's way of saying 'I'll give you a value eventually — it's not ready yet, but I promise.' Instead of blocking everything while waiting for data, a Promise lets you say 'when this is done, do this next thing.' It has three states: pending, resolved (fulfilled), or rejected.

Real Talk

A Promise is a JavaScript object representing the eventual completion or failure of an asynchronous operation. It provides .then() for success handlers, .catch() for error handlers, and .finally() for cleanup. Promises form the foundation of async/await syntax and are used throughout browser APIs (Fetch, IndexedDB, etc.).

Show Me The Code

// Creating a Promise
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms))

// Chaining
fetch('/api/data')
  .then(res => res.json())
  .then(data => console.log(data))
  .catch(err => console.error(err))

// Promise.all — run in parallel
const [users, posts] = await Promise.all([fetchUsers(), fetchPosts()])

When You'll Hear This

Promise.all lets you run multiple async operations in parallel.,A rejected Promise without a .catch() causes an unhandled rejection error.,Async/await is just syntactic sugar over Promises.

Made with passive-aggressive love by manoga.digital. Powered by Claude.