GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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 Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.