#35: Async 🔀


Grand Central Dispatch is a wonderful way to write asynchronous code in your app. It's actual API however, can sometimes be a bit, well, let's call it "dry". Async is a Swift library by Tobias Due Munk that adds some much-welcomed syntactic sugar to Grand Central Dispatch. Async's core API is simple enough to be understood in a single line of code, so let's look at some other fun things it can do:

Chain Blocks

Async.background {
  // Runs on the background queue
}.main {
  // Runs on the main queue, but only after
  // the previous block returns

Delayed Execution

Async.main(after: 0.5) {
  // executed on the main queue after 500 ms
}.background(after: 0.4) {
  // executed on the background queue
  // 400 ms after the first block completes

Cancel Blocks

let computeThings = Async.background {

let computerOtherThings = computeThings.background {
  // this sucker is about to get cancelled

Async.main {
  computeThings.cancel() // this won't do anything

  // you can only cancel blocks that haven't yet started executing!

  // this second block *would* in fact get cancelled,
  // since it hasn't started executing yet

More info about Async can be found at