We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Creating Coroutines

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 data
  1. async def always returns a coroutine object, even if there's no await inside.
  2. The await keyword can only be used inside an async function or at top level in Pyodide.
  3. A coroutine doesn't run until you await it or schedule it, e.g. with asyncio.run()

Assignment

The get_issue_data function can't await the pyfetch call.