Challenge - 5 Problems
Async Sequence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this async sequence code?
Consider this Swift async sequence that emits numbers 1 to 3 with a delay. What will be printed?
iOS Swift
import Foundation func printNumbers() async { for await number in AsyncStream<Int>({ continuation in Task { for i in 1...3 { try? await Task.sleep(nanoseconds: 100_000_000) continuation.yield(i) } continuation.finish() } }) { print(number) } } Task { await printNumbers() }
Attempts:
2 left
💡 Hint
AsyncStream yields values in order, and the for-await loop prints each as it arrives.
✗ Incorrect
The AsyncStream yields numbers 1, 2, and 3 with a small delay. The for-await loop prints each number as it is received, resulting in 1, 2, 3 printed on separate lines.
❓ lifecycle
intermediate1:30remaining
What happens if you do not call continuation.finish() in AsyncStream?
In Swift's AsyncStream, what is the effect of omitting the call to continuation.finish() after yielding values?
Attempts:
2 left
💡 Hint
Think about how the async sequence knows it is done.
✗ Incorrect
Without calling continuation.finish(), the AsyncStream does not signal completion, so the for-await loop waits indefinitely for more values.
📝 Syntax
advanced2:00remaining
Which option correctly creates an AsyncStream of strings?
Choose the correct Swift code snippet that creates an AsyncStream emitting the strings "A", "B", "C".
Attempts:
2 left
💡 Hint
Check which option yields all values and calls finish properly.
✗ Incorrect
Option A correctly yields all strings and calls continuation.finish() to end the stream. Option A yields numbers as strings but not the required letters. Option A misses calling finish(), so the stream never ends. Option A is valid but yields values directly without iteration; however, the question asks for the correct snippet emitting "A", "B", "C" which option A does clearly with iteration.
🔧 Debug
advanced2:00remaining
What error occurs with this AsyncSequence code?
Given this Swift code snippet, what error will it produce when run?
iOS Swift
func test() async { let stream = AsyncStream<Int> { continuation in continuation.yield(1) continuation.yield(2) // Missing finish call } for await value in stream { print(value) } } Task { await test() }
Attempts:
2 left
💡 Hint
What happens if the stream never signals completion?
✗ Incorrect
Because continuation.finish() is missing, the AsyncStream never signals it is done. The for-await loop prints 1 and 2 but then waits forever for more values, causing the code to hang.
🧠 Conceptual
expert2:30remaining
How does AsyncSequence handle backpressure in Swift concurrency?
In Swift's concurrency model, how does AsyncSequence manage backpressure when producing values faster than the consumer can process?
Attempts:
2 left
💡 Hint
Think about how async/await suspends tasks in Swift.
✗ Incorrect
Swift's AsyncSequence suspends the producing task when the consumer is not ready, effectively applying backpressure and preventing unbounded buffering or dropping values.