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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.