Asynchronous Swift

Adam Eri
blackmirror
Published in
1 min readJun 1, 2017

When writing asynchronous code, you normally use closures or delegate methods to return data. But it tends to get a bit messy, when you have to perform a series of these async operations.

If you are familiar with JavaScript and the ES8 proposal for async/await, or maybe even used the awesome colibrary, you are going to love AwaitKit.

Copied with pride from their readme:

Build on top of PromiseKit, AwaitKit really changes the way you handle async operations and makes the code more clean — well, mostly. Furthermore, with the help of PromiseKit, you are able to wrap any asynchronous system, built-in or 3-rd party.

Here a brilliant post on why async/await is better then promises.

Now, you do not need promises or await for a single operation. We setup our async methods to support any of these ‘resolution` methods.

This allows you to handle the response any way you prefer.

Performance

So how much does this cost? I have ran a simple test to return a single string with all three different versions.

Closure measured [Time, seconds]: average: 0.000, values: [0.000613, 0.000433, 0.000419, 0.000391, 0.000407, 0.000396, 0.000784, 0.000596, 0.000262, 0.000266]

Promise measured [Time, seconds]: average: 0.001, values: [0.001453, 0.000851, 0.000944, 0.000732, 0.000702, 0.000779, 0.000581, 0.001618, 0.000991, 0.001151]

Async/Await measured [Time, seconds] average: 0.002, values: [0.008311, 0.000725, 0.000980, 0.000860, 0.000808, 0.000658, 0.000667, 0.000780, 0.000642, 0.000940]

--

--

Adam Eri
blackmirror

A software architect building apps and games for Apple platforms and cloud based micro-service solutions.