Bird
0
0

You want to fetch user info and user posts concurrently using async/await in Swift. Which code correctly runs both tasks at the same time and waits for both results?

hard📝 Application Q15 of 15
iOS Swift - Concurrency
You want to fetch user info and user posts concurrently using async/await in Swift. Which code correctly runs both tasks at the same time and waits for both results?
Alet user = await fetchUser() let posts = await fetchPosts()
BfetchUser() fetchPosts() print("Done")
Casync let user = fetchUser() async let posts = fetchPosts() let userInfo = await user let userPosts = await posts
DTask { let user = fetchUser() let posts = fetchPosts() print(user, posts) }
Step-by-Step Solution
Solution:
  1. Step 1: Understand concurrent async calls

    Using async let starts tasks concurrently without waiting immediately.
  2. Step 2: Await both results

    Awaiting user and posts later waits for both to finish, running in parallel.
  3. Final Answer:

    async let user = fetchUser() async let posts = fetchPosts() let userInfo = await user let userPosts = await posts -> Option C
  4. Quick Check:

    async let runs tasks concurrently = B [OK]
Quick Trick: Use async let to run tasks in parallel [OK]
Common Mistakes:
  • Awaiting tasks one after another (sequential)
  • Not awaiting async let variables
  • Calling async functions without await

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes