Recall & Review
beginner
What does the
await keyword do in Swift?The
await keyword pauses the current function until an asynchronous task finishes, allowing other code to run meanwhile.Click to reveal answer
beginner
Can you use
await outside an asynchronous function in Swift?No,
await can only be used inside functions marked with async.Click to reveal answer
intermediate
How does
await improve app responsiveness?await lets the app do other work while waiting for slow tasks, like network calls, so the app stays smooth and doesn’t freeze.Click to reveal answer
intermediate
What happens if you forget to use
await when calling an async function?The async function returns a task immediately without waiting for the result, which can cause bugs if you expect the result right away.
Click to reveal answer
beginner
Example: What does this code do?
<pre>func fetchData() async {
let data = await download()
print(data)
}</pre>This function waits for
download() to finish before printing the data. The await pauses fetchData() until download() completes.Click to reveal answer
Where can you use the
await keyword in Swift?✗ Incorrect
await must be inside async functions to pause and wait for asynchronous tasks.What does
await do when calling an async function?✗ Incorrect
await pauses the current function until the async function completes and returns a result.What happens if you call an async function without
await?✗ Incorrect
Calling an async function without
await returns a task immediately, not the final result.Why is using
await good for app performance?✗ Incorrect
await allows the app to stay responsive by letting other tasks run while waiting for slow operations.Which keyword must a function have to use
await inside it?✗ Incorrect
Only functions marked with
async can use await to wait for asynchronous tasks.Explain in your own words how the
await keyword works in Swift and why it is useful.Think about how waiting for slow tasks can freeze an app and how <code>await</code> helps avoid that.
You got /4 concepts.
Describe a simple example where you would use
await in a Swift app.Imagine fetching data from the internet and needing to wait for it before showing it.
You got /4 concepts.