anext() Asynchronous next

Last updated: 2026-09-22

anext() Asynchronous next

anext(async_iterator[, default]) returns the next item from an async iterator.

CategoryBuilt-in Functions
KindBuilt-in Function
Python Version3.10+

📝 Syntax

anext(async_iterator[, default])

⚙️ Parameters

async_iterator RequiredAn async iterator.
default OptionalValue 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

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

1
2
end
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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏