anext() Asynchronous next
Last updated: 2026-09-22
anext() Asynchronous next
anext(async_iterator[, default]) returns the next item from an async iterator.
| Category | Built-in Functions |
|---|---|
| Kind | Built-in Function |
| Python Version | 3.10+ |
📝 Syntax
anext(async_iterator[, default])
⚙️ Parameters
| async_iterator Required | An async iterator. |
|---|---|
| default Optional | Value returned when the async iterator is exhausted and no fallback is given. |
Returns:The next item from the async iterator; returns default if exhausted and default is given.
💥 Raises
StopAsyncIteration— Raised when the async iterator is exhausted and no default is given.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
1
2
end
🏷️ Related Built-in Functions
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat is the difference between anext(it) and next(it)?
Anext() is synchronous and returns the next item directly; anext() works on async iterators, whose __anext__() returns a coroutine that must be awaited to get the value: await anext(it). The pairing mirrors iter↔next and aiter↔anext.
QWhat happens when the async iterator is exhausted?
ARaises StopAsyncIteration when no default is given; if the second parameter default is provided (e.g. anext(it, 0)), it returns default instead of raising.
QMust anext be used in an async context?
AEssentially yes. __anext__() returns a coroutine, so you normally await it inside an async function; async for calls it automatically. The function can be called anywhere, but the returned value must be awaited to get a result.
QHow to safely fetch items when not using default?
AWrap await anext(it) in try/except StopAsyncIteration; or simpler, iterate directly with async for, which handles exhaustion internally.