asyncEasy Examples

Declares an asynchronous coroutine function or context manager

Async function basics

Defining and running an async function.

python
import asyncio

async def greet(name):
    print(f"Hello, {name}!")
    return f"Greeted {name}"

# Run the async function
result = asyncio.run(greet("Python"))
print(f"Result: {result}")

# Async with await
async def fetch_data():
    print("Fetching...")
    await asyncio.sleep(0)  # simulate async work
    print("Done!")
    return [1, 2, 3]

data = asyncio.run(fetch_data())
print(f"Data: {data}")
Expected Output
Hello, Python!
Result: Greeted Python
Fetching...
Done!
Data: [1, 2, 3]

async def creates a coroutine function. Calling it returns a coroutine object that must be awaited or run with asyncio.run().

Want to try these examples interactively?

Open Easy Playground