Skip to content

async/await support #63

Open
Open
@carson-katri

Description

@carson-katri

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions