Skip to content

Task Continuations

Question

How can you perform an action after a task finishes without awaiting it? Can ContinueWith run only after success?

Short interview answer

You can attach a continuation with ContinueWith, including OnlyOnRanToCompletion, but await is usually clearer for normal control flow. If the work must outlive the current request, use an explicit background-work design rather than fire-and-forget code.

Detailed answer

ContinueWith exposes scheduling and status options, but it is easier to misuse than await, especially around contexts and exceptions. A continuation restricted to successful completion will not run after a fault or cancellation. Any unawaited operation needs an explicit strategy for error observation, cancellation, lifetime, and shutdown.

For example, await SendReceiptAsync(order); await MarkReceiptSentAsync(order.Id); expresses that the second operation follows successful completion of the first. A continuation API is only needed when its scheduling or completion-state behavior is specifically required.

Sources