Problem when learning to apply async/await

My job is port my code from js style AwaitKit to Swift 5.5 async/await. Say I have a func like this

func download() async throws {
    let (data, response) = try await URLSession.shared.data(for:someURL)
}

ASWebAuthenticationSession.CompletionHandler
typealias CompletionHandler = (URL?, Error?) -> Void

However, I have to call the download function from ASWebAuthenticationSession.CompletionHandler, which does not allow to call async functions.

When using AwaitKit, the func is allowed to be called. Any ideas?

1 Like

However, I have to call the download function from
ASWebAuthenticationSession.CompletionHandler, which does not allow
to call async functions.

I don’t think this is different from any other context when you need to start async work in a context that’s not async. If so, the standard approach is to create a new top-level task:

Task {
    await … whatever …
}

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

2 Likes

I think Apple just forgot to provide the async APIs as well. For example, there are new beta async API for WKNavigationDelegate, like this webView(_:decidePolicyFor:decisionHandler:)

The answer @eskimo provided worked!!

Terms of Service

Privacy Policy

Cookie Policy