/python-future-then

Extension to Python's Future (Promise) API that allows chaining using .then() (like JavaScript Promises)

Primary LanguagePythonMIT LicenseMIT

WHAT & WHY

Extending Python's concurrent.futures.Future (Promise) object with missing chaining API like .then().

While Futures (aka Promises) are formally specified and available as part of Python as of v 3.2+, and is available as backported module for Python versions v.2.x, standard implementation is lacking any formal or convenient API for the most frequent use of Promises - chaining.

JavaScript Promises/A+ API have then-ability as part of the spec. This project takes standard concurrent.futures.Future Python class and extends it with borrowed .then() behavior from JavaScript Promises.

INSTALL

pip install futures_then

USE

In [1]: from futures_then import ThenableFuture as Future

In [2]: f1 = Future()

In [3]: f2 = f1.then(lambda v: v + ' transformed')

In [4]: f1.set_result('future')

In [5]: f2.result()
Out[5]: 'future transformed'