@@ -47,15 +47,16 @@ list_ext.extend()
4747Currently, we provide the following extensions:
4848
4949
50- | file | extended types |
51- | :---------------:| :----------------------------------:|
52- | dict_ext.py | dict_keys, dict_values, dict_items |
53- | float_ext.py | float |
54- | function_ext.py | FunctionType, LambdaType |
55- | int_ext.py | int |
56- | list_ext.py | list |
57- | seq_ext.py | map, filter, range, zip |
58- | str_ext.py | str |
50+ | file | extended types |
51+ | :----------------:| :----------------------------------:|
52+ | coroutine_ext.py | coroutine (async functions) |
53+ | dict_ext.py | dict_keys, dict_values, dict_items |
54+ | float_ext.py | float |
55+ | function_ext.py | FunctionType, LambdaType |
56+ | int_ext.py | int |
57+ | list_ext.py | list |
58+ | seq_ext.py | map, filter, range, zip |
59+ | str_ext.py | str |
5960
6061
6162
@@ -214,6 +215,36 @@ list.last(self: List[T]) -> T, raise IndexError
214215```
215216Returns the last element in the list, or raises ` IndexError ` if the list is empty.
216217
218+ ``` py
219+ coroutine.then(self : Awaitable[T], fn: Callable[[T], Awaitable[U] | U]) -> Awaitable[U]
220+ ```
221+ Maps the result of the awaitable via an optionally async function. If the function is async, it is awaited in the context of the wrapped awaitable.
222+
223+ Example:
224+ ``` py
225+ async def get_value ():
226+ return 10
227+
228+ result = await get_value().then(lambda x : x * 2 ) # result is 20
229+ ```
230+
231+ ``` py
232+ coroutine.catch(self : Awaitable[T], fn: Callable[[E], Awaitable[U] | U], * , exception: type[E] = Exception ) -> Awaitable[T | U]
233+ ```
234+ Catches an exception of the given type and calls the passed function with the caught exception.
235+
236+ If no exception was raised inside the wrapped awaitable, the function will not be called.
237+ The passed function can optionally return a value to be returned in case of an error.
238+ The passed function can be either sync or async. If it's async, it is awaited in the context of the wrapped awaitable.
239+
240+ Example:
241+ ``` py
242+ async def might_fail ():
243+ raise ValueError (" error" )
244+
245+ result = await might_fail().catch(lambda e : " default" , exception = ValueError ) # result is "default"
246+ ```
247+
217248``` py
218249float .round(self : float ) -> int
219250```
0 commit comments