From a0b417253fefa94fad6bb94b37b109d079700486 Mon Sep 17 00:00:00 2001 From: Anshusinghh Date: Tue, 14 Oct 2025 18:16:11 +0530 Subject: [PATCH 1/3] Added [Term Entry] Python keywords: await #7736 --- .../concepts/keywords/terms/await/await.md | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 content/python/concepts/keywords/terms/await/await.md diff --git a/content/python/concepts/keywords/terms/await/await.md b/content/python/concepts/keywords/terms/await/await.md new file mode 100644 index 00000000000..6918b89b01a --- /dev/null +++ b/content/python/concepts/keywords/terms/await/await.md @@ -0,0 +1,74 @@ +--- +Title: 'await' +Description: 'Pauses the execution of an async function until the awaited coroutine completes.' +Subjects: + - 'Computer Science' + - 'Web Development' +Tags: + - 'Async' + - 'Await' + - 'Python' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +The **`await`** keyword pauses the execution of an asynchronous function until the awaited [coroutine](https://docs.python.org/3/library/asyncio-task.html#id2), task, or awaitable object is completed. It allows asynchronous code to be written in a way that resembles synchronous code. + +## Syntax + +```pseudo +await expression +``` + +The `await` keyword can only be used inside an `async` function. Using it outside an async function will raise a `SyntaxError`. + +## Example + +The await keyword can be used to wait for an asynchronous task before continuing: + +```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 the Example: + +- `Async`: It is used to define an asynchronous function. +- `Asyncio`: It provides the framework and utilities to write asynchronous programs, such as event loops, tasks, and coroutines. + + +## Codebyte Example + +Below is another example: + +```codebyte/python +import asyncio + +async def greet(): + await asyncio.sleep(1) + return "Hello from await!" + +async def main(): + message = await greet() + print(message) + +asyncio.run(main()) +``` \ No newline at end of file From ab80a45684f0303071d1616fd76c5c7408dab9e8 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 16 Oct 2025 15:34:42 +0530 Subject: [PATCH 2/3] minor fixes with the content --- .../concepts/keywords/terms/await/await.md | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/content/python/concepts/keywords/terms/await/await.md b/content/python/concepts/keywords/terms/await/await.md index 6918b89b01a..c9c3d3e8925 100644 --- a/content/python/concepts/keywords/terms/await/await.md +++ b/content/python/concepts/keywords/terms/await/await.md @@ -1,19 +1,19 @@ --- Title: 'await' -Description: 'Pauses the execution of an async function until the awaited coroutine completes.' +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' + - 'Async Await' + - 'Functions' - 'Python' CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- -The **`await`** keyword pauses the execution of an asynchronous function until the awaited [coroutine](https://docs.python.org/3/library/asyncio-task.html#id2), task, or awaitable object is completed. It allows asynchronous code to be written in a way that resembles synchronous code. +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 @@ -21,54 +21,61 @@ The **`await`** keyword pauses the execution of an asynchronous function until t await expression ``` -The `await` keyword can only be used inside an `async` function. Using it outside an async function will raise a `SyntaxError`. +> **Note:** The `await` keyword can only be used inside an `async` function. Using it outside an async function will raise a `SyntaxError`. ## Example -The await keyword can be used to wait for an asynchronous task before continuing: +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} + print("Fetching data...") + await asyncio.sleep(1) + print("Data received!") + return {"data": 42} async def main(): - result = await fetch_data() - print("Result:", result) + result = await fetch_data() + print("Result:", result) asyncio.run(main()) ``` + Here is the output: ```shell - Fetching data... - Data received! - Result: {'data': 42} +Fetching data... +Data received! +Result: {'data': 42} ``` -In the Example: -- `Async`: It is used to define an asynchronous function. -- `Asyncio`: It provides the framework and utilities to write asynchronous programs, such as event loops, tasks, and coroutines. +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. -## Codebyte Example +## Example -Below is another example: +This example waits for one second before returning and printing a message: -```codebyte/python +```py import asyncio async def greet(): - await asyncio.sleep(1) - return "Hello from await!" + await asyncio.sleep(1) + return "Hello from await!" async def main(): - message = await greet() - print(message) + message = await greet() + print(message) asyncio.run(main()) -``` \ No newline at end of file +``` + +The output of this code is: + +```shell +Hello from await! +``` From 26357ac6e78e5cb0edff0de992b471764dcdeb66 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 16 Oct 2025 15:35:13 +0530 Subject: [PATCH 3/3] Update await.md --- content/python/concepts/keywords/terms/await/await.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/python/concepts/keywords/terms/await/await.md b/content/python/concepts/keywords/terms/await/await.md index c9c3d3e8925..82556c0fa05 100644 --- a/content/python/concepts/keywords/terms/await/await.md +++ b/content/python/concepts/keywords/terms/await/await.md @@ -23,7 +23,7 @@ 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 +## Example 1 This example pauses execution while waiting for simulated data to be fetched: @@ -56,7 +56,7 @@ 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 +## Example 2 This example waits for one second before returning and printing a message: