Skip to content

Instantly share code, notes, and snippets.

@koher
Last active May 4, 2017 23:30
Show Gist options
  • Save koher/df007741788cb197d65b6babe2acf480 to your computer and use it in GitHub Desktop.
Save koher/df007741788cb197d65b6babe2acf480 to your computer and use it in GitHub Desktop.

throws : async = try : await = catch : wait = rethrows : reasync = Result<T> : Promise<T>

throws : async = try : await

// 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([...])
  ...
}

do / wait

// Without `async` because of `wait`
func foo() -> Int {
  do {
    return await actionSheet([...])
  } wait // Blocking
}

Asyncronous error handling

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)
}

reasync

// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment