Bird
0
0

Which Swift code snippet correctly runs two async tasks concurrently and waits for both results before proceeding?

hard📝 Application Q9 of 15
iOS Swift - Concurrency
Which Swift code snippet correctly runs two async tasks concurrently and waits for both results before proceeding?
Alet first = await fetchFirst() let second = await fetchSecond() print(first, second)
Basync let first = fetchFirst() async let second = fetchSecond() let results = await (first, second)
CTask { let first = fetchFirst() let second = fetchSecond() print(await first, await second) }
Dlet first = fetchFirst() let second = fetchSecond() print(first, second)
Step-by-Step Solution
Solution:
  1. Step 1: Understand concurrency with async let

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

    Awaiting the tuple (first, second) waits for both tasks to complete.
  3. Step 3: Analyze other options

    let first = await fetchFirst() let second = await fetchSecond() print(first, second) runs tasks sequentially; C incorrectly mixes Task and awaits; D does not await at all.
  4. Final Answer:

    async let first = fetchFirst() async let second = fetchSecond() let results = await (first, second) -> Option B
  5. Quick Check:

    Use async let for concurrency, await tuple for results [OK]
Quick Trick: Use async let to start tasks concurrently [OK]
Common Mistakes:
  • Awaiting tasks sequentially instead of concurrently
  • Not awaiting async tasks before using results
  • Misusing Task without awaiting properly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes