UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: why not async var?

Forums > 100 Days of Swift

Hello,

I am unable to understand the arguments put forward on why not async var here.

That attempts to create a variable asynchronously, then writes to it directly. Have we cancelled the async work? If not, when the async work completes will it overwrite our new value? Do we still need to use await when reading the value even after we’ve explicitly set it?

Can someone please explain (w.r.t. the quote) why not async var?

2      

let makes the variable a constant. Once set you can't change its value anymore. A async function could take a while to finish. In the example shown and if we could change the value of the async var it would not be clear which value username would contain. The result of the function (Taylor Swift) or Justin Bieber.

The let prevents this.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.