Discover async let


You’re more of a video kind of person? I’ve got you covered! Here’s a video with the same content than this article 🍿


I want to show you a very easy way to run two asynchronous calls concurrently!

Take a look at this code:

I’m making 2 separate asynchronous calls to retrieve various pieces of data about a User.

This code seems perfectly reasonable at first glance.

But if we look closer, we can notice that the 2 calls are made sequentially: the second call starts only after the first one has finished.

But there’s actually no reason to run these two calls sequentially.

Since the second call doesn’t depend on the result of the first one, nothing prevents us from running them concurrently.

And it’s actually quite easy to do: we just need to use the async let syntax.

With this new syntax, both calls will immediately start executing concurrently!

You can notice that the keyword await has been moved.

It is normal: with this new syntax we will only await when we actually want to use the result of the asynchronous calls 👌

That’s all for this article, I hope you’ve enjoyed it!

Here’s the code if you want to experiment with it:

import Foundation

Task {
    async let userName = getUserName()
    async let userPicture = getUserPicture()

    await updateUI(userName: userName, userPicture: userPicture)
}
Previous
Previous

Here are 5 tools that will improve your iOS project 🛠

Next
Next

When do you really need to use [weak self]? 🤨