diff --git a/fastapi_cache/decorator.py b/fastapi_cache/decorator.py index d24c565..b203bb3 100644 --- a/fastapi_cache/decorator.py +++ b/fastapi_cache/decorator.py @@ -66,11 +66,15 @@ def cache( async def ensure_async_func(*args, **kwargs): """Run cached sync functions in thread pool just like FastAPI.""" if inspect.iscoroutinefunction(func): - # async, return as is - return func(*args, **kwargs) + # async, return as is. + # unintuitively, we have to await once here, so that caller + # does not have to await twice. See + # https://stackoverflow.com/a/59268198/532513 + return await func(*args, **kwargs) else: # sync, wrap in thread and return async - return run_in_threadpool(func, *args, **kwargs) + # see above why we have to await even although caller also awaits. + return await run_in_threadpool(func, *args, **kwargs) copy_kwargs = kwargs.copy()