Python await
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.
Syntax
await expression
Note: The
awaitkeyword can only be used inside anasyncfunction. Using it outside an async function will raise aSyntaxError.
Example 1
This example pauses execution while waiting for simulated data to be fetched:
import asyncioasync 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:
asyncdefines an asynchronous function that can containawait.asyncioprovides 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 asyncioasync 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
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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