Bird
0
0

What is wrong with this Swift async/await code snippet?

medium📝 Debug Q6 of 15
iOS Swift - Concurrency
What is wrong with this Swift async/await code snippet?
func fetchData() async -> String {
  return "Data"
}

func execute() {
  let data = await fetchData()
  print(data)
}
ACannot use 'await' outside an async context
BMissing return type in fetchData()
CfetchData() should not be async
Dprint statement syntax is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Identify async context requirement

    The await keyword can only be used inside an async function or closure.
  2. Step 2: Check function execute()

    The function execute() is not marked async, so using await inside it causes a compile error.
  3. Step 3: Correct usage

    Mark execute() as async or call await fetchData() inside an async context.
  4. Final Answer:

    Cannot use 'await' outside an async context -> Option A
  5. Quick Check:

    Await requires async function context [OK]
Quick Trick: Await only works inside async functions [OK]
Common Mistakes:
  • Using await in synchronous functions
  • Forgetting to mark caller function as async
  • Assuming async functions can be called synchronously

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes