How does Task.sleep differ from asyncAfter?
The essence
Task.sleep(for:)— an asynchronous pause inside the current task. The task suspends, the thread returns to the pool; after the pause the code continues from the same place, on the same isolation. The pause is cancellable: if the task is cancelled,Task.sleepimmediately throwsCancellationError. It works in any task — structured or created viaTask { }.DispatchQueue.asyncAfter(deadline:)— GCD schedules a closure on a queue no earlier than the given time. The caller continues immediately; the closure is a separate unit of work outside the task tree, with no cancellation (unless wrapped in aDispatchWorkItemandcancel()ed before it starts) and no inherited isolation/priority.
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep