Modern Concurrency: Getting Started

Oct 18 2022 · Swift 5.5, iOS 15, Xcode 13.4

Part 2: Asynchronous Sequences

10. Concurrency With async let

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 09. Your Second Asynchronous App Next episode: 11. Using Asynchronous Methods in Views

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Refresh your browser to make sure the course server is running or restart the server in Terminal

Serial vs Concurrent

When you store files on a cloud server, you usually want to know how much storage you’ve used and whether you have duplicate files. This “cloud server” has an endpoint to supply this information.

func status() async throws -> String {
  guard let url = URL(string: "http://localhost:8080/files/status") else {
    throw "Could not create the URL."
  }
  return ""
}
let (data, response) = try await URLSession.shared.data(from: url) // fetch the data
guard (response as? HTTPURLResponse)?.statusCode == 200 else {  // check the statusCode
  throw "The server responded with an error."
}
return String(decoding: data, as: UTF8.self)
do {
  files = try await model.availableFiles()
  🟩status = try await model.status()

Grouping async calls

OK, now back to that do closure.

async let files = try model.availableFiles()
async let status = try model.status()
let (filesResult, statusResult) = try await (files, status)
self.files = filesResult
self.status = statusResult