Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async/await support #63

Open
carson-katri opened this issue Jan 1, 2022 · 0 comments · May be fixed by #64
Open

async/await support #63

carson-katri opened this issue Jan 1, 2022 · 0 comments · May be fixed by #64
Labels
enhancement New feature or request

Comments

@carson-katri
Copy link
Owner

carson-katri commented Jan 1, 2022

The basic idea is that you can await any Request using call():

let getTodos = AnyRequest<[Todo]> {
  Url("todos.com/api")
}
let todos = try await getTodos.call()

callAsFunction could also be added to simplify this demo to:

let todos = try await getTodos()

This opens up new opportunities for crafting readable APIs:

enum API {
  // These can be used as async throwing functions:
  static let getPosts = AnyRequest<[Post]> {
    Url("...")
  }
  static let getUsers = AnyRequest<[User]> {
    Url("...")
  }
  // Real functions can also be added:
  static func getPost(id: Int) async throws -> Post {
    try await AnyRequest<Post> {
      Url(".../\(id)")
    }()
  }
}

// Simply use these as functions:
let posts = try await API.getPosts()
let users = try await API.getUsers()
let post = try await API.getPost(id: 0)

async let gives the power of concurrency:

async let posts = API.getPosts()
async let users = API.getUsers()
let authored = zip(try await callTodos, try await callAsFunctionTodos)
@carson-katri carson-katri added the enhancement New feature or request label Jan 1, 2022
@carson-katri carson-katri linked a pull request Jan 1, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant