how can I manually complete an asynchronous task while awaiting it?
Asked on 06/11/2025
1 search
To manually complete an asynchronous task while awaiting it in Swift, you can use the withCheckedContinuation
or withCheckedThrowingContinuation
functions. These functions allow you to bridge between callback-based APIs and Swift's async/await syntax. Here's a brief explanation of how you can use them:
-
withCheckedContinuation
: This function is used when you have a non-throwing asynchronous operation. You can call this function to create a continuation, which you can then resume once your asynchronous operation completes. -
withCheckedThrowingContinuation
: This is similar towithCheckedContinuation
, but it's used for operations that can throw errors. You can resume the continuation with either a success or a failure.
Here's a simple example of how you might use withCheckedContinuation
:
func performAsyncOperation() async -> ResultType {
return await withCheckedContinuation { continuation in
// Start your asynchronous operation here
someAsyncFunction { result in
// Once the operation completes, resume the continuation
continuation.resume(returning: result)
}
}
}
This approach allows you to manually control when the asynchronous task is considered complete by resuming the continuation at the appropriate time.
For more detailed information on using continuations in Swift, you might want to check out the session "Go further with Swift Testing" from WWDC 2024, which discusses using continuations to convert completion handlers to async/await syntax. You can find more about this topic in the Go further with Swift Testing (24:26) session.

A Swift Tour: Explore Swift’s features and design
Learn the essential features and design philosophy of the Swift programming language. We’ll explore how to model data, handle errors, use protocols, write concurrent code, and more while building up a Swift package that has a library, an HTTP server, and a command line client. Whether you’re just beginning your Swift journey or have been with us from the start, this talk will help you get the most out of the language.

Embracing Swift concurrency
Join us to learn the core Swift concurrency concepts. Concurrency helps you improve app responsiveness and performance, and Swift is designed to make asynchronous and concurrent code easier to write correctly. We’ll cover the steps you need to take an app through from single-threaded to concurrent. We’ll also help you determine how and when to make the best use of Swift concurrency features – whether it’s making your code more asynchronous, moving it to the background, or sharing data across concurrent tasks.

Finish tasks in the background
Discover background execution advancements and understand how the system schedules runtime. We’ll discuss how to get the most out of background runtime to allow your app to deliver features in the background while maintaining a great foreground experience. We’ll also cover how APIs provide background runtime for your app, and how each API is tailored for different use cases — including new APIs in iOS and iPadOS 26 that let your app finish tasks as your app transitions from the foreground to the background.