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
- Is used by: Async and await, Async stream, Task (async)
- An alternative to: Callback
- See also: Async and await, Asynchrony, Concurrency primitives, Join, Polling
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¶
- In this library: Getting a result back
- In a sibling library: Go: A goroutine has no handle ↗
- In the books: Learning Concurrent Programming in Scala, Aleksandar Prokopec — ch. 4, 'Asynchronous Programming with Futures and Promises'
- In the books: Parallel Programming with Python, Jan Palach — ch. 4, 'Using the threading and concurrent.futures Modules'
- In the books: JavaScript Concurrency, Adam Boduch — ch. 3, 'Synchronizing with Promises'
- In the books: The Art of Multiprocessor Programming, Maurice Herlihy, Nir Shavit — ch. 16, 'Futures, Scheduling, and Work Distribution'
- In the books: Asynchronous Programming in Rust, Carl Fredrik Samson — ch. 2, 'How Programming Languages Model Asynchronous Program Flow' → 'Coroutines: promises and futures'
- In the books: C++ Concurrency in Action, Anthony Williams — ch. 4, 'Synchronizing concurrent operations' → 'Waiting for one-off events with futures'
- Notes: future - promise - delay - deferred - general ↗
- Notes: futures - rust - main ↗
- Notes: Futures rust - code snippets ↗
- Notes: resolving - fulfilling, binding a future - general ↗
- Notes: asynchronous results - general ↗
- Notes: promises - general ↗
- Reference: Wikipedia: Futures and promises ↗