Bird
0
0

You want to migrate a Flask app to async and have a route that calls an async database query function async def fetch_data(). Which is the correct way to write the route?

hard📝 Application Q15 of 15
Flask - Ecosystem and Patterns
You want to migrate a Flask app to async and have a route that calls an async database query function async def fetch_data(). Which is the correct way to write the route?
Adef data(): result = fetch_data(); return result
Basync def data(): result = await fetch_data(); return result
Casync def data(): result = fetch_data(); return result
Ddef data(): result = await fetch_data(); return result
Step-by-Step Solution
Solution:
  1. Step 1: Understand async function calls

    To call an async function like fetch_data(), you must use await inside an async function.
  2. Step 2: Match route definition with async call

    The route must be async to use await. async def data(): result = await fetch_data(); return result correctly defines async route and awaits the async call.
  3. Final Answer:

    async def data(): result = await fetch_data(); return result -> Option B
  4. Quick Check:

    Async route + await async call = correct [OK]
Quick Trick: Use async def and await for async calls inside routes [OK]
Common Mistakes:
MISTAKES
  • Calling async function without await
  • Using await in sync functions
  • Defining sync route for async calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes