Challenge - 5 Problems
Async Dependency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this FastAPI async dependency?
Consider this FastAPI async generator dependency. What will the endpoint return when called?
FastAPI
from fastapi import FastAPI, Depends app = FastAPI() async def async_dep(): yield "hello" @app.get("/test") async def test(dep: str = Depends(async_dep)): return {"message": dep}
Attempts:
2 left
💡 Hint
Remember that async generator dependencies yield values that are passed to the endpoint parameter.
✗ Incorrect
The async generator dependency yields the string "hello", which is passed as the value of the 'dep' parameter. The endpoint returns it in a JSON response.
❓ lifecycle
intermediate2:00remaining
When is the cleanup code in an async generator dependency executed?
Given this async generator dependency, when will the cleanup code after the yield run?
FastAPI
from fastapi import FastAPI, Depends app = FastAPI() async def resource_dep(): print("Setup resource") yield "resource" print("Cleanup resource") @app.get("/use") async def use_resource(res: str = Depends(resource_dep)): return {"used": res}
Attempts:
2 left
💡 Hint
Think about how async generators work with yield and what happens after yield.
✗ Incorrect
In FastAPI, the code after yield in an async generator dependency runs after the endpoint function completes, allowing cleanup of resources.
📝 Syntax
advanced2:00remaining
Which option correctly defines an async generator dependency in FastAPI?
Select the option that correctly defines an async generator dependency that yields a database session.
Attempts:
2 left
💡 Hint
Async generator dependencies must use yield and can have cleanup code after yield.
✗ Incorrect
Option C correctly defines an async generator dependency with yield and cleanup after yield. Option C is not async, so FastAPI treats it as a normal function, which is invalid for async cleanup. Option C uses return instead of yield, so no cleanup runs. Option C incorrectly awaits a synchronous close method.
🔧 Debug
advanced2:00remaining
Why does this async generator dependency cause a runtime error?
Examine the code and select the reason for the runtime error when calling the endpoint.
FastAPI
from fastapi import FastAPI, Depends app = FastAPI() async def broken_dep(): resource = open("file.txt") yield resource resource.close() @app.get("/read") async def read_file(f = Depends(broken_dep)): return {"content": f.read()}
Attempts:
2 left
💡 Hint
Think about mixing synchronous blocking calls inside async functions.
✗ Incorrect
Opening a file synchronously inside an async generator blocks the event loop, which can cause runtime warnings or errors in async frameworks like FastAPI.
🧠 Conceptual
expert3:00remaining
What happens if an async generator dependency raises an exception after yield?
In FastAPI, if an async generator dependency yields a value but then raises an exception during cleanup code after yield, what is the effect on the endpoint response?
Attempts:
2 left
💡 Hint
Consider how FastAPI handles exceptions in dependency cleanup after the endpoint returns.
✗ Incorrect
If an exception occurs in the cleanup code after yield, FastAPI treats it as an error and returns a 500 Internal Server Error response, interrupting normal response flow.