Async/Await in swift

Updated 29 December 2021

Save

Async/Await is part of the new concurrency system. WWDC comes with new features Async/Await is one of them. Async function allows us to run complex code asynchronously.

Why do we need Async/Await function?

Commonly, we use a completion handler to handle the result, when we are making requests to fetch or push data.

For example, if one request needs to make after another one is completed. we need to put the second request in the completion handler of the first request.

What if we have multiple requests to execute in a specific order?

We end up something like this.

There is nothing wrong with the code, it’s just a little bit complex to write and understand.

That’s why we need Async/Await function

Asynchronous Functions

To create an async function we need to add the async keyword after the parameters:

To call the async function we need to add await keyword at the front of the request to execute the request in a specific order.

await keyword work only if the calling function should be an asynchronous function

Above all, functions will be executed after the previous functions are completed with the await keyword.

In conclusion, async function working is similar to the completion handler, but many times completion handler make our code difficult to understand, on the other hand, asynchronous functions clean up code and makes it more readable

I hope this blog helps you to understand the basics of an asynchronous function.

Lastly, Please share your thoughts with us in the comment section.

For more blogs please click here

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home