Hacker News new | ask | show | jobs
by dagmx 1453 days ago
Without Swift Concurrency, you'd write something like this (pseudocode):

GetWeather() { results in

   // Do something with the results

   UpdateUI(results)
}

The part in the curly braces is a closure/completion handler. The GetWeather call is not blocking, so it runs on another thread and then it calls your completion code when it's done.

This looks fine in a small example, but it quickly gets gnarly when you need to pipe those results to something else, or you can also end up deep in many completion handlers.

With the new concurrency model it's just this

let results = await GetWeather ()

UpdateUI(results)

Now let's say your UpdateUI call was also non blocking:

GetWeather() { results in

   UpdateUI(results) {

       DoSomethingElse ()

   }
}

Which just becomes this

let results = await GetWeather ()

await UpdateUI(results)

DoSomethingElse()