Complete the code to define an async FastAPI route handler.
from fastapi import FastAPI app = FastAPI() @app.get("/hello") async def hello(): return {"message": "Hello, World!"} # This is an [1] function
The route handler must be declared with async to support asynchronous execution in FastAPI.
Complete the code to await an async function inside a FastAPI route.
from fastapi import FastAPI import asyncio app = FastAPI() async def fetch_data(): await asyncio.sleep(1) return "data" @app.get("/data") async def get_data(): result = [1] fetch_data() return {"result": result}
To get the result of an async function, you must use await inside an async function.
Fix the error in the FastAPI route by choosing the correct decorator for an async function.
from fastapi import FastAPI app = FastAPI() [1]("/items") async def read_items(): return ["item1", "item2"]
The route should use @app.get decorator to handle GET requests for reading items.
Fill both blanks to create an async FastAPI route that returns JSON with a delay.
from fastapi import FastAPI import asyncio app = FastAPI() @app.[1]("/wait") async def wait_and_return(): await asyncio.[2](2) return {"status": "done"}
The route uses @app.get for GET requests and asyncio.sleep to pause asynchronously.
Fill all three blanks to create a FastAPI async route that processes query parameters and returns a JSON response.
from fastapi import FastAPI, Query app = FastAPI() @app.get("/search") async def search_items(q: str = [1](..., min_length=[2])): return {"query": q, "length": [3](q)}
Use Query to declare query parameters with validation, min_length=3 to require at least 3 characters, and len to get the length of the query string.