Commit 5d50e341 authored by Nathan Graule's avatar Nathan Graule 💻
Browse files

Add `from_function` classmethod that creates a Call from a synchronous function

parent baf55fc2
from threading import Thread
from typing import Callable, Any, TypeVar
from typing import Callable, Any, TypeVar, Optional
__all__ = ['Call']
......@@ -103,6 +103,23 @@ class Call:
error = Exception(error)
return Call(lambda res, rej: rej(error))
@classmethod
def from_function(cls, func, *args, **kwargs):
# type: (Callable[[Any], T], *Any, **Any) -> Call
"""Create a Call from a synchronous function. The function will then be called asynchronously, its return
value used as the resolved value, and any exception raised as a reject error value.
:param func: Synchronous function to be called
:param args: Positional arguments to be passed to the function func
:param kwargs: Dictionary arguments to be passed to the function func"""
def cb(resolve, reject):
# type: (Callable, Callable) -> None
try:
resolve(func(*args, **kwargs))
except Exception as e:
reject(e)
return Call(cb)
def _on_resolve(self, data):
# type: (T) -> None
"""DO NOT USE. IS INTERNAL"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment