Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions content/python/concepts/keywords/terms/await/await.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
Title: 'await'
Description: 'Suspends the execution of an asynchronous function until the awaited coroutine, task, or awaitable object finishes.'
Subjects:
- 'Computer Science'
- 'Web Development'
Tags:
- 'Async Await'
- 'Functions'
- 'Python'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

The **`await`** keyword suspends the execution of an asynchronous function until the awaited [coroutine](https://docs.python.org/3/library/asyncio-task.html#id2), task, or awaitable object completes. It allows asynchronous code to look and behave like synchronous code.

## Syntax

```pseudo
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:

```py
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:

```shell
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:

```py
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:

```shell
Hello from await!
```