pypromise 0.4
Promises/A+ implementation for Python
Promise
This is a implementation of Promises in Python. It is a super set of Promises/A+ designed to have readable, performant code and to provide just the extensions that are absolutely necessary for using promises in Python.
Its fully compatible with the Promises/A+ spec
Installation
$ pip install pypromise
Usage
The example below shows how you can load the promise library. It then demonstrates creating a promise from scratch. You simply call Promise(fn). There is a complete specification for what is returned by this method in Promises/A+.
from promise import Promise promise = Promise( lambda resolve, reject: resolve('RESOLVED!') )
API
Before all examples, you will need:
from promise import Promise
Promise(resolver)
This creates and returns a new promise. resolver must be a function. The resolver function is passed two arguments:
- resolve should be called with a single argument. If it is called with a non-promise value then the promise is fulfilled with that value. If it is called with a promise (A) then the returned promise takes on the state of that new promise (A).
- reject should be called with a single argument. The returned promise will be rejected with that argument.
Static Functions
These methods are invoked by calling Promise.methodName.
Promise.resolve).
Promise.reject(value)
Returns a rejected promise with the given value.
Promise.all(list)
Returns a promise for a list. If it is called with a single argument then this returns a promise for a copy of that list with any promises replaced by their fulfilled values. e.g.
p = Promise.all([Promise.resolve('a'), 'b', Promise.resolve('c')]) \ .then(lambda res: res == ['a', 'b', 'c']) assert p.value is True
Instance Methods
These methods are invoked on a promise instance by calling myPromise.methodName
promise.then(on_fulfilled, on_rejected)
This method follows the Promises/A+ spec. It explains things very clearly so I recommend you read it.
Either on_fulfilled or on_rejected will be called and they will not be called more than once. They will be passed a single argument and will always be called asynchronously (in the next turn of the event loop).
If the promise is fulfilled then on_fulfilled is called. If the promise is rejected then on_rejected.
promise.catch(on_rejected)
Sugar for promise.then(None, on_rejected), to mirror catch in synchronous code.
promise.done(on_fulfilled, on_rejected)
The same semantics as .then except that it does not return a promise and any exceptions are re-thrown so that they can be logged (crashing the application in non-browser environments)
Other package functions
is_thenable(obj)
This function checks if the obj is a Promise, or could be promisifyed.
promisify(obj)
This function wraps the obj act as a Promise if possible. Python Futures are supported, with a callback to promise.done when resolved.
Notes
This package is heavily insipired in aplus.
License
- Author: Syrus Akbary
- Download URL:
- Keywords: concurrent future deferred promise
- License: MIT
- Categories
- Development Status :: 5 - Production/Stable
- Intended Audience :: Developers
- License :: OSI Approved :: MIT License
- Programming Language :: Python :: 2
- Programming Language :: Python :: 2.7
- Programming Language :: Python :: 3
- Programming Language :: Python :: 3.3
- Programming Language :: Python :: 3.4
- Programming Language :: Python :: 3.5
- Programming Language :: Python :: Implementation :: PyPy
- Topic :: Software Development :: Libraries
- Package Index Owner: syrusakbary
- DOAP record: pypromise-0.4.xml