await_if_needed¶
-
async
privex.helpers.asyncx.
await_if_needed
(func: Union[callable, Coroutine, Awaitable, Any], *args, **kwargs)[source]¶ Call, await, and/or simply return
func
depending on whether it’s an async function reference (coroutine function), a non-awaited coroutine, a standard synchronous function, or just a plain old string.Helps take the guess work out of parameters which could be a string, a synchronous function, an async function, or a coroutine which hasn’t been awaited.
>>> def sync_func(hello, world=1): ... return f"sync hello: {hello} {world}" >>> async def async_func(hello, world=1): ... return f"async hello: {hello} {world}" >>> await await_if_needed(sync_func, 3, world=2) 'sync hello: 3 2' >>> await await_if_needed(async_func, 5, 4) 'async hello: 5 4' >>> f = async_func(5, 4) >>> await await_if_needed(f) 'async hello: 5 4'
- Parameters
func (callable|Coroutine|Awaitable|Any) – The function/object to await/call if needed.
args – If
func
is a function/method, will forward any positional arguments to the functionkwargs – If
func
is a function/method, will forward any keyword arguments to the function
- Return Any func_data
The result of the awaited
func
, or the originalfunc
if not a coroutine nor callable/awaitable