Implement a Promise / Future
⏳ What is a promise? A promise (also called a future) is a box for a result that is not ready yet. One piece of code promises to fill the box later, with a value or with an error. Anyone holding the box can wait for the result, check whether it has arrived, or leave a note saying "when it arrives, do this next".
Java's CompletableFuture, JavaScript's Promise and Python's concurrent.futures.Future are all versions of the same design. An interviewer who asks you to "implement a future", "build a promise with then", or "write allOf and anyOf" is asking for one object with three parts: a write-once result, a way to block on it with a timeout, and a list of callbacks that must each run exactly once. We cover all of them here, and the timeout variants come up again in Extensibility.
The problem is short, but almost every line is a concurrency decision. It is a good test of whether you can find the race in "check, then act" code, and whether you know why callbacks should never run while you hold a lock.
Requirements
"Implement a Promise class. Some thread completes it with a value or an error. Other threads can wait for the result, and you can chain work with
then. Also give meallOfandanyOf."
The prompt is small on purpose. The questions below decide the shape of the class, so ask them before you write anything.
Clarifying Questions
You: What type of value does the promise hold?
Interviewer: Keep it to integers for this exercise. In a real library it would be generic.
You: What if two threads try to complete the same promise?
Interviewer: The first one wins. Later calls do nothing and return false, so the caller knows it lost.
You: How are errors represented, and what happens to a then chain when something fails?
Interviewer: A promise can fail with an error message. If a promise fails, the function given to then is skipped and the next promise fails with the same error. If that function throws, the next promise fails with the exception's message.
You: Which thread runs the callbacks? Do we have a thread pool?
Interviewer: No pool. A callback runs on whichever thread settles the promise. If the promise is already settled when you call then, run it right away on the calling thread.
You: How does a reader wait, and what does it get if the result never comes?
Interviewer: get takes a timeout in milliseconds. It returns the value, -2 if the promise failed, or -1 if it timed out. Values are never negative, so those codes are safe.
You: What exactly should allOf and anyOf return?
Interviewer: allOf completes with the sum of the values once all inputs complete, and fails as soon as any input fails. anyOf completes with the first value that arrives, and fails only if every input fails, with the last error.
You: Do we need cancellation?
Interviewer: Not now.
The fourth answer matters most. "Run the callback on the settling thread" means the code that completes a promise also runs other people's code. That is where the deadlocks hide, and we will spend a whole section on it.
💡 Interview tip: When a design takes callbacks, always ask "which thread runs them?". The answer (the caller, the completer, or a pool) changes the locking story completely, and asking it shows you have been bitten by it before.
Final Requirements
- A promise starts pending and settles exactly once:
complete(value)orfail(error). The first call wins and returnstrue; later calls returnfalseand change nothing. is_done()tells whether the promise has settled.get(timeout_ms)blocks until the promise settles or the timeout runs out. It returns the value,-2for a failure, or-1for a timeout. Many threads may wait at once, and all of them wake when it settles.error()returns the failure message, or"".then(fn)returns a new promise. When this one completes withv, the new one completes withfn(v). Iffnthrows, the new one fails with the exception's message. If this one fails,fnis skipped and the error passes along.- Every callback runs exactly once, even if it is added at the same moment another thread settles the promise. On an already settled promise it runs right away.
all_of(promises)completes with the sum once all inputs complete, or fails with the first error.any_of(promises)completes with the first value, or fails with the last error once all inputs fail.
Out of scope
- Cancellation and interrupting the work behind a promise.
- Thread pools and "async" callback variants (we come back to both in Extensibility).
- Generic value types and returning a list from
all_of.