diff --git a/Week05/awaitme_bilal_ayakdas.py b/Week05/awaitme_bilal_ayakdas.py new file mode 100644 index 00000000..1ab0c9c8 --- /dev/null +++ b/Week05/awaitme_bilal_ayakdas.py @@ -0,0 +1,22 @@ +import asyncio + +def awaitme(fn): + """ + Decorator to ensure synchronous and asynchronous functions can be awaited consistently. + + This decorator wraps a function, checking if it returns a coroutine. If so, it awaits the coroutine, + allowing synchronous functions to run as usual while enabling asynchronous support. + + :param fn: The function to be decorated. Can be synchronous or asynchronous. + :type fn: callable + :return: A coroutine function that awaits the result if needed. + :rtype: coroutine + + """ + + async def convert_to_coroutine(*args, **kwargs): + _func = fn(*args, **kwargs) + if asyncio.iscoroutine(_func): + return await _func + return _func + return convert_to_coroutine