aioplus.afirst

async aioplus.afirst(aiterable, /, *, default=<enum 'Sentinel'>)

Return the first item of the aiterable.

Parameters:
  • aiterable (AsyncIterable[T]) – An asynchronous iterable to retrieve the item from.

  • default (D, optional) – A default value to return if the iterable is empty. If not provided, IndexError will be raised if the iterable is empty.

Returns:

The first item of aiterable or the default value.

Return type:

T or D

Examples

>>> import asyncio
>>>
>>> from aioplus import afirst, arange
>>>
>>> async def main() -> None:
>>>     '''Run the program.'''
>>>     aiterable = arange(4, 23)
>>>     num = await afirst(aiterable)
>>>     print(f'aiterable[0] = {num}')
>>>
>>> if __name__ == '__main__':
>>>     asyncio.run(main())