logo

Promises

Understanding Promises, chaining, and common patterns for error handling and sequencing.

Short explanation

Promises represent the future result of an asynchronous operation. They can be pending, fulfilled, or rejected, and they make async control flow manageable via .then() and .catch() chains.

Syntax example

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

How JS handles it internally

Promises schedule their callbacks to run as microtasks after the current call stack clears. Engines manage the microtask queue to ensure predictable ordering relative to macrotasks.

FAQ

Q: When should I use Promises vs callbacks?

A: Prefer Promises (or async/await). They avoid callback hell and provide better error propagation.