How to fix the error âasync call in a function that does not support concurrencyâ
How to fix the error âasync call in a function that does not support concurrencyâ êŽë š
Updated for Xcode 15
This error occurs when youâve tried to call an async function from a synchronous function, which is not allowed in Swift â asynchronous functions must be able to suspend themselves and their callers, and synchronous functions simply donât know how to do that.
If your asynchronous work needs to be waited for, you donât have much of a choice but to mark your current code as also being async
so that you can use await
as normal. However, sometimes this can result in a bit of an âasync infectionâ â you mark one function as being async, which means its caller needs to be async too, as does *its*
caller, and so on, until youâve turned one error into 50.
In this situation, you can create a dedicated Task
to solve the problem. Weâll be covering this API in more detail later on, but hereâs how it would look in your code:
func doAsyncWork() async {
print("Doing async work")
}
func doRegularWork() {
Task {
await doAsyncWork()
}
}
doRegularWork()
Tasks like this one are created and run immediately. We arenât waiting for the task to complete, so we shouldnât use await
when creating it.