Package: promise
PromiseThis 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 thePromises/A+
specInstallation$ pip install promiseUsageThe example below shows how you can load the promise library. It then
demonstrates creating a promise from scratch. You simply callPromise(fn). There is a complete specification for what is returned
by this method inPromises/A+.frompromiseimportPromisepromise=Promise(lambdaresolve,reject:resolve('RESOLVED!'))APIBefore all examples, you will need:frompromiseimportPromisePromise(resolver)This creates and returns a new promise.resolvermust be a function.
Theresolverfunction is passed two arguments:resolveshould 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).rejectshould be called with a single argument. The returned
promise will be rejected with that argument.Class MethodsThese methods are invoked by callingPromise.methodName.Promise.resolve(value)Converts values and foreign promises into Promises/A+ promises. If you
pass it a value then it returns a Promise for that value. If you pass it
something that is close to a promise (such as a jQuery attempt at a
promise) it returns a Promise that takes on the state ofvalue(rejected or fulfilled).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(lambdares:res==['a','b','c'])assertp.get()isTruePromise.cast(obj)This function wraps theobjact as aPromiseif possible. PythonFutures are supported, with a callback topromise.donewhen
resolved. Have the same effects asPromise.resolve(obj).Promise.for_dict(d)A special function that takes a dictionary of promises and turns them
into a promise for a dictionary of values. In other words, this turns an
dictionary of promises for values into a promise for a dictionary of
values.Promise.is_thenable(obj)This function checks if theobjis aPromise, or could becasted.Promise.promisify(func)This function wraps the result of callingfuncin aPromiseinstance.Instance MethodsThese methods are invoked on a promise instance by callingmyPromise.methodNamepromise.then(did_fulfill, did_reject)This method follows thePromises/A+
spec. It explains
things very clearly so I recommend you read it.Eitherdid_fulfillordid_rejectwill 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 thendid_fulfillis called. If the
promise is rejected thendid_rejectis called.The call to.thenalso returns a promise. If the handler that is
called returns a promise, the promise returned by.thentakes on the
state of that returned promise. If the handler that is called returns a
value that is not a promise, the promise returned by.thenwill be
fulfilled with that value. If the handler that is called throws an
exception then the promise returned by.thenis rejected with that
exception.promise.catch(did_reject)Sugar forpromise.then(None, did_reject), to mirrorcatchin
synchronous code.promise.done(did_fulfill, did_reject)The same semantics as.thenexcept 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)ContributingAfter cloning this repo, ensure dependencies are installed by running:pipinstall-e".[test]"After developing, the full test suite can be evaluated by running:py.testtests--cov=promise--benchmark-skip# Use -v -s for verbose modeYou can also run the benchmarks with:py.testtests--benchmark-onlyStatic type checkingPython type annotations are very useful for making sure we use the
libary the way is intended.You can runmypystatic type checker:pipinstallmypymypypromise--ignore-missing-importsOrpyre:pipinstallpyre-checkpyre--source-directorypromisecheckNotesThis package is heavily insipired inaplus.LicenseMIT
License