Python await

Anonymous contributor's avatar
Anonymous contributor
Published Oct 22, 2025
Contribute to Docs

The await keyword pauses the execution of an asynchronous function until the awaited coroutine, task, or awaitable object completes. It lets asynchronous code read and behave more like synchronous code.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

await expression

Note: The await keyword can only be used inside an async function. Using it outside an async function will raise a SyntaxError.

Example 1

This example pauses execution while waiting for simulated data to be fetched:

import asyncio
async def fetch_data():
print("Fetching data...")
await asyncio.sleep(1)
print("Data received!")
return {"data": 42}
async def main():
result = await fetch_data()
print("Result:", result)
asyncio.run(main())

Here is the output:

Fetching data...
Data received!
Result: {'data': 42}

In this example:

  • async defines an asynchronous function that can contain await.
  • asyncio provides tools for running asynchronous code, such as event loops, coroutines, and tasks.

Example 2

This example waits for one second before returning and printing a message:

import asyncio
async def greet():
await asyncio.sleep(1)
return "Hello from await!"
async def main():
message = await greet()
print(message)
asyncio.run(main())

The output of this code is:

Hello from await!

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours