Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 680 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'async' call in a function that does not support concurrency

#1
I'm trying to use async/await with Swift 5.5. I have my async function, but whenever I try to call it, I get this error:

> 'async' call in a function that does not support concurrency

Here's the code sample:

```swift
class TryThis {
func getSomethingLater(_ number: Double) async -> String {
// test - sleep for 3 seconds, then return
Thread.sleep(forTimeInterval: 3)
return String(format: ">>>%8.2f<<<", number)
}
}

let tryThis = TryThis()

let result = await tryThis.getSomethingLater(3.141592653589793238462)
print("result: \(result)")
```

What's the solution for this??
Reply

#2
When calling `async` code (including `actor` fields) from non-`async` code, you have to wrap it in a `Task`:

```swift
Task {
let result = await tryThis.getSomethingLater(3.141592653589793238462)
print("result: \(result)")
}
```

If you need it to escape that async context, you can use traditional callbacks:

```swift
func awaitedResult(callback: @escaping (String) -> Void) {
Task {
let result = await tryThis.getSomethingLater(3.141592653589793238462)
callback(result)
}
}

awaitedResult { result in
print("result: \(result)")
}
```

[Swift by Sundell](

[To see links please register here]

) also offers some alternatives involving Combine

---

Further reading: [Concurrency — The Swift Programming Language (Swift 5.5)](

[To see links please register here]

)
Reply

#3
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.

func doAsyncWork() async {
print("Doing async work")
}

func doRegularWork() {
Task {
await doAsyncWork()
}
}

doRegularWork()



[Reference here][1]


[1]:

[To see links please register here]

Reply

#4
The answer here is that the "await getSomethingLater" must be called from an async context. Literally that means changing this:

```swift
let result = await tryThis.getSomethingLater(3.141592653589793238462)
print("result: \(result)")
```

into this:

```swift
Task {
let result = await tryThis.getSomethingLater(3.141592653589793238462)
print("result: \(result)")
}
```

So the whole thing becomes:
```swift
class TryThis {
func getSomethingLater(_ number: Double) async -> String {
// test - sleep for 3 seconds, then return
Thread.sleep(forTimeInterval: 3)
return String(format: ">>>%8.2f<<<", number)
}
}

let tryThis = TryThis()

Task {
let result = await tryThis.getSomethingLater(3.141592653589793238462)
print("result: \(result)")
}
```
Here's the output:

> result: >>> 3.14<<<

There's great info in the [Meet async/await in Swift][1] video from WWDC21.


[1]:

[To see links please register here]

Reply

#5
In my case I just refactored the:

async { some-code-which-can-take-some-time }

with:

Task { some-code-which-can-take-some-time }

Reply

#6
The original error is produced because "top-level" await is not yet supported in Swift (that is, code at the global scope).
The error just means you need to provide the async function with an asynchronous context, like using `Task`, `Task.detached` or a `TaskGroup`, depending on the behaviour you want.

```swift
Task.detached {
let result = await tryThis.getSomethingLater(3.141592653589793238462)
print("result: \(result)")
}
```

However, your code snippet should work in the future when top-level await is eventually proposed, implemented and supported.
Top-level await support was mentioned as part of the original proposal for async/await [SE-0296](

[To see links please register here]

).
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through