Package: async-promises
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 async_promises in
Python.This was forked to make it fully compatible with thePromise/A+
specOn completion ofpromise PR #20,
maintenence for this fork will be dropped.Installation$ pip install async_promisesUsageThe example below shows how you can load the async_promises library. It then
demonstrates creating a async_promises from scratch. You simply callPromise(fn). There is a complete specification for what is returned
by this method inPromise/A+.fromasync_promisesimportPromiseasync_promises=Promise(lambdaresolve,reject:resolve('RESOLVED!'))APIBefore all examples, you will need:fromasync_promisesimportPromisePromise(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 Promise/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.rejected(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.promisify(obj)This function wraps theobjact as aPromiseif possible. PythonFutures are supported, with a callback toasync_promises.donewhen
resolved.Promise.for_dict(d)A special function that takes a dictionary of Promises and turns them
into a Promises for a dictionary of values. In other words, this turns an
dictionary of Promises for values into a Promises for a dictionary of
values.Instance MethodsThese methods are invoked on a Promise instance by callingmyPromise.methodNameasync_promises.then(on_fulfilled, on_rejected)This method follows thePromise/A+
spec. It explains
things very clearly so I recommend you read it.Eitheron_fulfilledoron_rejectedwill 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 thenon_fulfilledis called. If the
Promise is rejected thenon_rejectedis 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.async_promises.catch(on_rejected)Sugar forasync_promises.then(None, on_rejected), to mirrorcatchin
synchronous code.async_promises.done(on_fulfilled, on_rejected)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)Other package functionsis_thenable(obj)This function checks if theobjis aPromise, or could bepromisifyed.NotesThis package is heavily insipired inaplus.LicenseMIT
License