Built-in Functions and Types
anext()
Request an asynchronous iterator’s next item through its __anext__ method. The call produces an awaitable; await it to obtain the value. Exhaustion raises StopAsyncIteration unless a default was supplied. Other errors still propagate. Do not confuse an async iterable with the iterator returned by aiter().
Formal Syntax & Structure
python syntaxpython
anext(async_iterator, /)anext(async_iterator, default, /)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| async_iterator | asynchronous iterator | Object supporting __anext__; positional only. |
| default | object | Value returned on exhaustion when explicitly supplied; positional only. |
Return Value
awaitable
Awaiting yields the next item or the supplied default, or raises an exception.
Working Example
Example Snippetpython
import asyncio
async def batches(): yield "ready"
async def main(): iterator = aiter(batches()) print(await anext(iterator)) print(await anext(iterator, "finished")) try: await anext(iterator) except StopAsyncIteration: print("exhausted")
asyncio.run(main())