

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Synchronous vs. Asynchronous
incomplete
2: Why Do We Want Async Code?
incomplete
3: Coroutines & the Event Loop
incomplete
4: Why Are Coroutines Useful?
incomplete
5: Using Coroutines
incomplete
6: Creating Coroutines
incomplete
This lesson's interactive features are locked, please to keep using them
While the await keyword is used to wait for a coroutine to complete, the async keyword is used to create a coroutine function. When we call an async function, it returns a coroutine object that can later be awaited.
async def get_issue_data(issue_id: str) -> dict[str, str]:
resp = await pyfetch(f"/api/issues/{issue_id}")
return await resp.json()
coroutine = get_issue_data("ISS‑42") # returns immediately – nothing has executed yet
result = await coroutine # now the coroutine runs and yields the dataasync def always returns a coroutine object, even if there's no await inside.
The await keyword can only be used inside an async function or at top level in Pyodide.
A coroutine doesn't run until you await it or schedule it, e.g. with asyncio.run()
The get_issue_data function can't await the pyfetch call.