Function Details: anext
Description
Returns the next item from an async iterator.
Extended Description
The anext() function is used to retrieve the next item from an async iterator. If a default value is provided and the iterator is exhausted, it returns the default value instead of raising a StopAsyncIteration exception. This function is the asynchronous counterpart to the next() function used for synchronous iterators.
Exceptions
- StopAsyncIteration: If the iterator is exhausted and no default value is provided
- TypeError: If the first argument is not an async iterator
Read More about anext from Python Documentation
Function Signature
anext(async_iterator: AsyncIterator, default: Any = None) -> Any
Module: builtins
Parameters
This function takes two parameters: async_iterator (an AsyncIterator object), and default (optional, default is None). The default value is returned if the iterator is exhausted.
Parameter List
- async_iterator: AsyncIterator
- default: Any
Return
Returns the next item from the async iterator. If the iterator is exhausted and no default is provided, it raises StopAsyncIteration.
Return Type
Any
Output
Explanation
This example demonstrates basic usage of anext() with an async generator. It shows how to retrieve values and handle StopAsyncIteration when the iterator is exhausted.