Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. `throws` : `async` = `try` : `await` = `rethorws` : `reasync` = `Result<T>` : `Promise<T>`
  2.  
  3. ## `throws` : `async` = `try` : `await`
  4.  
  5. ```swift
  6. // Shows an action sheet and makes select one item
  7. func actionSheet(itemes: [String]) async -> Int { ... }
  8.  
  9. // Calls an `async` function in an `async` function
  10. func foo() async {
  11. let selectedIndex = await actionSheet([...])
  12. ...
  13. }
  14.  
  15. // Calls an `async` function outside `async` functions
  16. func onClick() {
  17. do {
  18. let selectedIndex = await actionSheet([...])
  19. ...
  20. } defer // does not block
  21. }
  22. ```
  23.  
  24. ## Asyncronous error handling
  25.  
  26. ```swift
  27. func download(url: URL) async throws -> Data { ... }
  28.  
  29. do {
  30. let data = await try download(url)
  31. ...
  32. } catch error {
  33. ...
  34. } defer
  35. ```
  36.  
  37. ## `reasync`
  38.  
  39. ```swift
  40. do {
  41. let r = await [2, 3, 5].map { (x: Int) async -> Int in ... }
  42. ...
  43. } defer
  44. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement