throws
: async
= try
: await
= catch
: wait
= rethrows
: reasync
= Result<T>
: Promise<T>
// Asynchronous selection by an action sheet
func actionSheet(itemes: [String]) async -> Int { ... }
// `await` must be in an `async` function
func foo() async {
let selectedIndex = await actionSheet([...])
...
}
// Without `async` because of `wait`
func foo() -> Int {
do {
return await actionSheet([...])
} wait // Blocking
}
func download(url: URL) async throws -> Data { ... }
do {
let data = await try download(url)
...
} catch let error {
...
} wait
or
func foo(url: URL) async -> Foo {
do {
let data = await try download(url)
return Foo(data: data)
} catch _ {
return Foo()
} // No `wait` here
}
or
func foo(url: URL) async throws -> Foo {
let data = await try download(url)
return Foo(data: data)
}
// in Array<Element>
func map<T>(_ transform: (Element) async throws -> T) reasync rethrows -> [T]
do {
let r = await [2, 3, 5].map { (x: Int) async -> Int in ... }
...
} wait