Skip to content

Future and promise

Category: Async · Status: stub · Lessons: chapter 06, Async (planned)

One line: A placeholder for a result that is not ready yet: the future is the side that waits for the value, the promise the side that supplies it.

Also called: future, promise, deferred, Task.

How it connects

flowchart LR
  n_async_await["Async and await"]
  n_async_stream["Async stream"]
  n_callback["Callback"]
  n_future_and_promise["Future and promise"]
  n_async_task["Task (async)"]
  n_async_await -->|uses| n_future_and_promise
  n_async_stream -->|uses| n_future_and_promise
  n_async_task -->|uses| n_future_and_promise
  n_callback ---|or| n_future_and_promise
  classDef center stroke-width:3px
  class n_future_and_promise center
  classDef outside stroke-dasharray: 4 3
  class n_async_await,n_async_stream,n_callback,n_async_task outside

In each language

Rust Future, a trait with a poll method; an async function's future does no work until polled (reference ↗); a thread's result comes back from JoinHandle::join
Go none: a go statement ↗ discards the function's return values, so a result has to be sent back on a channel
C++ std::future and std::promise (C++11); std::async returns a future
Java Future; a CompletableFuture can also be completed explicitly, so it is the promise side too
Python concurrent.futures.Future from executors; asyncio.Future on the event loop, which is not thread-safe
C# Task<TResult> is the future and TaskCompletionSource<TResult> the producer side
JavaScript Promise; the executor passed to its constructor ↗ runs synchronously and receives the resolve and reject functions
Kotlin Deferred, returned by async: a light-weight non-blocking future that is also a Job, so it can be cancelled
Swift a Task handle, whose result is read with await handle.value
Erlang and Elixir Task.async and Task.await: a task is a process meant to execute one particular action

Where to read more