Future and promise 00:00:00 Make a prediction You will distinguish a future from the authority to complete it and predict what a timeout says about work still running. A screen waits one second for a thumbnail, times out and shows a placeholder. Has the worker necessarily stopped generating the image? No. The waiter's deadline limits its own wait. The worker may still complete unless a separate cancellation protocol reaches it and the operation cooperates. 00:00:30 The glass case A hat maker gives two customers slips for the same glass case. The case is empty at first. When the hat is ready it goes inside and the case is sealed; both slips now point to the same result. A failed order can seal a failure note instead. The slips are futures readers can observe. The maker's authority to settle the case is the promise. A second completion must not overwrite the first. A future stands for a value that does not exist yet. It starts pending and is settled once: fulfilled with a value, or rejected with an error. Readers can wait for it, or leave a function that runs when it is settled. Waiting is expensive if a thread does it. With a future, the thread that asked can go on, and the result finds its way later. 00:01:19 Resolve one image request A caller requests a thumbnail and immediately receives a future. The worker holds the ability to complete the matching promise. On successful decoding it sets the result. On failure it records an error. Exactly one of these outcomes should settle the operation. Two consumers observing the same completed future should see the same outcome. A second attempted completion must follow a defined rule instead of silently replacing the original result. Let the renderer finish and attempt to complete its promise twice. The consumer should observe one terminal result under the chosen API. Now make rendering fail before producing bytes. The future must communicate that failure too, or callers can wait indefinitely for an impossible image. 00:02:09 Timing out is not cancellation A screen waits one second for a thumbnail and then displays a placeholder. Its wait may end while the worker continues rendering. To stop work, the operation needs a separate cancellation mechanism and the worker must cooperate. A timeout alone does not prove that no output will be produced. Have the caller stop waiting after one second but allow the worker to finish after two. The completed result may still be cached or discarded by policy. If you truly need cancellation, propagate a signal to the worker and define what it does when completion races with that signal. Starting a second load on every timeout can multiply work although the original request is still running. Decide when callers share results and how cancellation is coordinated. A synchronous return is clearer for an immediate value; a future earns its complexity when the result really arrives later. Java HttpClient. sendAsync returns at once with a CompletableFuture of the response, which completes when the response becomes available. AWS SDK for Java 2. Asynchronous clients return CompletableFuture objects. Methods have the same signatures as the synchronous ones otherwise. 00:03:31 Try an interview follow-up On which thread does a callback run? For a Java CompletableFuture, a non-async continuation can run on the completing thread, or during registration if the result is ready. Async methods schedule on an executor. In JavaScript, a Promise reaction runs in a later microtask, even if the promise already settled. A result arrives just before the caller attaches a callback. Should that completed operation lose its notification? A future retains its terminal state so late observers can still obtain the result. Specify where the continuation runs for the chosen runtime and executor. Then test both registration orders, including a callback that itself raises an error. 00:04:21 What to take with you A future is read, a promise is written once. Pending becomes fulfilled or rejected and stays so. Start independent work first and wait afterwards. Use thenApply for values, thenCompose for futures, and one handler at the end. Inside: change the state and take the list of callbacks in one step, call them after the lock. Every combinator is one new promise, a callback on each input and a count.