Skip to content

Commit a7c7747

Browse files
authored
Merge pull request #653 from Bilal-AYAKDAS/patch-6
Create awaitme_bilal_ayakdas.py
2 parents 22115b5 + b2aac09 commit a7c7747

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Week05/awaitme_bilal_ayakdas.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import asyncio
2+
3+
def awaitme(fn):
4+
"""
5+
Decorator to ensure synchronous and asynchronous functions can be awaited consistently.
6+
7+
This decorator wraps a function, checking if it returns a coroutine. If so, it awaits the coroutine,
8+
allowing synchronous functions to run as usual while enabling asynchronous support.
9+
10+
:param fn: The function to be decorated. Can be synchronous or asynchronous.
11+
:type fn: callable
12+
:return: A coroutine function that awaits the result if needed.
13+
:rtype: coroutine
14+
15+
"""
16+
17+
async def convert_to_coroutine(*args, **kwargs):
18+
_func = fn(*args, **kwargs)
19+
if asyncio.iscoroutine(_func):
20+
return await _func
21+
return _func
22+
return convert_to_coroutine

0 commit comments

Comments
 (0)