← Lessons

quiz vs the machine

Silver1050

Concurrency

The Future and Promise Abstraction

Two ends of one value that does not exist yet.

4 min read · intro · beat Silver to climb

A value that is not here yet

A future is a read only handle to a result that will arrive later. A promise is the matching write side: whoever owns the promise eventually fulfills it with a value or rejects it with an error.

Why split the two sides

  • The producer holds the promise and decides the outcome.
  • The consumer holds the future and only reads it.
  • This separation stops a reader from secretly completing someone else work.

A future has three states: pending, fulfilled, and rejected. Once it leaves pending it is settled and never changes again. That immutability is what makes futures safe to share across many readers.

Registering interest

Instead of blocking a thread, you attach a callback that fires when the future settles. The runtime stores the callback and invokes it once the promise is resolved. Multiple readers can attach to the same future and all of them are notified.

A common shape

The promise and future are often bundled into one object, as in JavaScript, where resolve and reject are passed to the executor.

Key idea

A future is the read side of a result that does not exist yet, and a promise is the write side that settles it exactly once.

Check yourself

Answer to earn rating on the learn ladder.

1. What is the difference between a future and a promise?

2. Once a future is settled, what can happen to its value?