Skip to content
Snippets Groups Projects
Commit 4989f216 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

added `Future.chain`

parent ae21fa37
No related branches found
Tags v2.12.6
No related merge requests found
# v2.12.6
* added `call_in_future`
* added `Future.chain`
......@@ -37,6 +37,21 @@ class Future(PythonFuture):
self._pre_done_callbacks = []
self._done_callbacks = []
def chain(self, fun):
"""
Schedule function to be called with the result of this future as it's argument (or
exception value if the future excepted).
:param fun: function to call
"""
def inner(future):
if future._exception is not None:
result = future._exception
else:
result = future._result
fun(result)
self.add_done_callback(inner)
def add_pre_done_callback(self, fn):
"""
Attaches a callable that will be called just before the future finishes
......
......@@ -55,6 +55,16 @@ class TestConcurrent(unittest.TestCase):
fut.set_result(3)
self.assertEqual(wrap.result(), 5)
def test_future_chain(self):
fut = Future()
def assert_five(arg):
self.assertEqual(arg, 5)
fut.chain(assert_five)
fut.set_running_or_notify_cancel()
fut.set_result(5)
def test_future(self):
fut = Future()
fut.set_running_or_notify_cancel()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment