• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

promise/H07-May-2022-1,7851,237

promise.egg-info/H03-May-2022-248175

LICENSEH A D18-Dec-20191.1 KiB2217

MANIFEST.inH A D18-Dec-201965 43

PKG-INFOH A D18-Dec-20198.8 KiB248175

README.rstH A D18-Dec-20196 KiB221149

setup.cfgH A D18-Dec-2019137 96

setup.pyH A D18-Dec-20191.9 KiB6555

README.rst

1Promise
2=======
3
4This is a implementation of Promises in Python. It is a super set of
5Promises/A+ designed to have readable, performant code and to provide
6just the extensions that are absolutely necessary for using promises in
7Python.
8
9Its fully compatible with the `Promises/A+
10spec <http://promises-aplus.github.io/promises-spec/>`__
11
12|travis| |pypi| |coveralls|
13
14Installation
15------------
16
17::
18
19    $ pip install promise
20
21Usage
22-----
23
24The example below shows how you can load the promise library. It then
25demonstrates creating a promise from scratch. You simply call
26``Promise(fn)``. There is a complete specification for what is returned
27by this method in
28`Promises/A+ <http://promises-aplus.github.com/promises-spec/>`__.
29
30.. code:: python
31
32    from promise import Promise
33
34    promise = Promise(
35        lambda resolve, reject: resolve('RESOLVED!')
36    )
37
38API
39---
40
41Before all examples, you will need:
42
43.. code:: python
44
45    from promise import Promise
46
47Promise(resolver)
48~~~~~~~~~~~~~~~~~
49
50This creates and returns a new promise. ``resolver`` must be a function.
51The ``resolver`` function is passed two arguments:
52
531. ``resolve`` should be called with a single argument. If it is called
54   with a non-promise value then the promise is fulfilled with that
55   value. If it is called with a promise (A) then the returned promise
56   takes on the state of that new promise (A).
572. ``reject`` should be called with a single argument. The returned
58   promise will be rejected with that argument.
59
60Class Methods
61~~~~~~~~~~~~~
62
63These methods are invoked by calling ``Promise.methodName``.
64
65Promise.resolve(value)
66^^^^^^^^^^^^^^^^^^^^^^
67
68Converts values and foreign promises into Promises/A+ promises. If you
69pass it a value then it returns a Promise for that value. If you pass it
70something that is close to a promise (such as a jQuery attempt at a
71promise) it returns a Promise that takes on the state of ``value``
72(rejected or fulfilled).
73
74Promise.reject(value)
75^^^^^^^^^^^^^^^^^^^^^
76
77Returns a rejected promise with the given value.
78
79Promise.all(list)
80^^^^^^^^^^^^^^^^^
81
82Returns a promise for a list. If it is called with a single argument
83then this returns a promise for a copy of that list with any promises
84replaced by their fulfilled values. e.g.
85
86.. code:: python
87
88    p = Promise.all([Promise.resolve('a'), 'b', Promise.resolve('c')]) \
89           .then(lambda res: res == ['a', 'b', 'c'])
90
91    assert p.get() is True
92
93Promise.cast(obj)
94^^^^^^^^^^^^^^^^^
95
96This function wraps the ``obj`` act as a ``Promise`` if possible. Python
97``Future``\ s are supported, with a callback to ``promise.done`` when
98resolved. Have the same effects as ``Promise.resolve(obj)``.
99
100Promise.for\_dict(d)
101^^^^^^^^^^^^^^^^^^^^
102
103A special function that takes a dictionary of promises and turns them
104into a promise for a dictionary of values. In other words, this turns an
105dictionary of promises for values into a promise for a dictionary of
106values.
107
108Promise.is\_thenable(obj)
109^^^^^^^^^^^^^^^^^^^^^^^^^
110
111This function checks if the ``obj`` is a ``Promise``, or could be
112``cast``\ ed.
113
114Promise.promisify(func)
115^^^^^^^^^^^^^^^^^^^^^^^
116
117This function wraps the result of calling ``func`` in a ``Promise``
118instance.
119
120Instance Methods
121~~~~~~~~~~~~~~~~
122
123These methods are invoked on a promise instance by calling
124``myPromise.methodName``
125
126promise.then(did\_fulfill, did\_reject)
127~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
128
129This method follows the `Promises/A+
130spec <http://promises-aplus.github.io/promises-spec/>`__. It explains
131things very clearly so I recommend you read it.
132
133Either ``did_fulfill`` or ``did_reject`` will be called and they will
134not be called more than once. They will be passed a single argument and
135will always be called asynchronously (in the next turn of the event
136loop).
137
138If the promise is fulfilled then ``did_fulfill`` is called. If the
139promise is rejected then ``did_reject`` is called.
140
141The call to ``.then`` also returns a promise. If the handler that is
142called returns a promise, the promise returned by ``.then`` takes on the
143state of that returned promise. If the handler that is called returns a
144value that is not a promise, the promise returned by ``.then`` will be
145fulfilled with that value. If the handler that is called throws an
146exception then the promise returned by ``.then`` is rejected with that
147exception.
148
149promise.catch(did\_reject)
150^^^^^^^^^^^^^^^^^^^^^^^^^^
151
152Sugar for ``promise.then(None, did_reject)``, to mirror ``catch`` in
153synchronous code.
154
155promise.done(did\_fulfill, did\_reject)
156^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
157
158The same semantics as ``.then`` except that it does not return a promise
159and any exceptions are re-thrown so that they can be logged (crashing
160the application in non-browser environments)
161
162Contributing
163============
164
165After cloning this repo, ensure dependencies are installed by running:
166
167.. code:: sh
168
169    pip install -e ".[test]"
170
171After developing, the full test suite can be evaluated by running:
172
173.. code:: sh
174
175    py.test tests --cov=promise --benchmark-skip # Use -v -s for verbose mode
176
177You can also run the benchmarks with:
178
179.. code:: sh
180
181    py.test tests --benchmark-only
182
183Static type checking
184--------------------
185
186Python type annotations are very useful for making sure we use the
187libary the way is intended.
188
189You can run ``mypy`` static type checker:
190
191.. code:: sh
192
193    pip install mypy
194    mypy promise  --ignore-missing-imports
195
196Or ``pyre``:
197
198.. code:: sh
199
200    pip install pyre-check
201    pyre --source-directory promise check
202
203Notes
204=====
205
206This package is heavily insipired in
207`aplus <https://github.com/xogeny/aplus>`__.
208
209License
210-------
211
212`MIT
213License <https://github.com/syrusakbary/promise/blob/master/LICENSE>`__
214
215.. |travis| image:: https://img.shields.io/travis/syrusakbary/promise.svg?style=flat
216   :target: https://travis-ci.org/syrusakbary/promise
217.. |pypi| image:: https://img.shields.io/pypi/v/promise.svg?style=flat
218   :target: https://pypi.python.org/pypi/promise
219.. |coveralls| image:: https://coveralls.io/repos/syrusakbary/promise/badge.svg?branch=master&service=github
220   :target: https://coveralls.io/github/syrusakbary/promise?branch=master
221