Bird
0
0

Identify the issue in this async middleware code:

medium📝 Debug Q6 of 15
Django - Async Django
Identify the issue in this async middleware code:
class CustomMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    async def __call__(self, request):
        response = self.get_response(request)
        return response
AMissing await before calling self.get_response(request)
BThe __init__ method should be async
CThe __call__ method should be synchronous
DMiddleware should not return a response
Step-by-Step Solution
Solution:
  1. Step 1: Check async call usage

    Since __call__ is async, calling self.get_response(request) must be awaited if it returns a coroutine.
  2. Step 2: Identify missing await

    Missing await causes the coroutine to not execute properly.
  3. Final Answer:

    Missing await before calling self.get_response(request) -> Option A
  4. Quick Check:

    Async calls require await to execute coroutines [OK]
Quick Trick: Always await async calls inside async methods [OK]
Common Mistakes:
MISTAKES
  • Forgetting to await async functions
  • Making __init__ async unnecessarily
  • Confusing sync and async __call__ signatures

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes