Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Open sidebar
Nathan Graule
call
Commits
5d50e341
Commit
5d50e341
authored
Aug 07, 2018
by
Nathan Graule
💻
Browse files
Add `from_function` classmethod that creates a Call from a synchronous function
parent
baf55fc2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
1 deletion
+18
-1
call/__init__.py
call/__init__.py
+18
-1
No files found.
call/__init__.py
View file @
5d50e341
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"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment