Node.js Course
Node.js
/
Intermediate

Promises

Definition

An object representing the eventual completion (or failure) of an asynchronous operation and its resulting value. It cures Callback Hell by allowing asynchronous methods to be chained.

Explain Like I'm New

A Promise is a restaurant buzzer. You order food, and they give you a physical buzzer object. It sits in your hand in a 'Pending' state. You can keep talking with your friends. Eventually, the buzzer either flashes green ('Resolved' - here is your food) or flashes red ('Rejected' - we ran out of ingredients).

Real World Example

Modern database drivers and the `fetch` API all return Promises. You attach `.then()` to handle success, and `.catch()` to handle errors cleanly in one place.

Common Use Cases

  • •Modern asynchronous flow control
  • •Fetching APIs
  • •Chaining dependent async tasks

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • What are the three states of a Promise?

intermediate

  • How does chaining `.then()` solve Callback Hell?

advanced

  • What does `Promise.all()` do?

Flash Cards

Question

What are the three states?

Click to reveal answer
Answer

Pending (waiting), Fulfilled/Resolved (success), and Rejected (failed).

Question

What does Promise.all() do?

Click to reveal answer
Answer

It takes an array of Promises and runs them concurrently (at the same time). It resolves only when ALL of them succeed. If even one fails, the entire `Promise.all` fails instantly. Great for fetching data from 3 APIs simultaneously.