Promise

class Promise(executor=None, reject=None)

A Promise modelled after the Promises/A+ standard

__init__(executor=None, reject=None)

This creates a Promise:

  • if reject is None, then executor is called with executor(promise_resolve, promise_reject) where reject and resolve are the callables to be invoked to indicate if the promise has to be resolved or rejected.

  • if reject is not None the invocation will be:

    • executor(promise_resolve)

    • reject(promise_reject)

If the execution during creation raises an Exception, the promise will be immediately rejected

static resolve(value)

This creates a Promise which is immediately resolved with value

static reject(value)

This creates a Promise which is immediately rejected with value

static all(*promises)

This creates a Promise which awaits the resolution of all promises passed as arguments. Anything which is not a promise is considered to be immediately resolved and the face value taken as the resolution value

If any of the promises is rejected, the promise will also be rejected with the value of the rejected promise.

static race(*promises)

This creates a Promise which waits until one of the promises passed as arguments *promises is resolved/rejected and takes the corresponding value for resolution/rejection

If no arguments are given, the promise will wait forever (because there is no meaningful value for either resolution or rejection)

If any of the arguments is not a promise, its value will be immediately used for resolving the returned promise.

Returns a promise

then(then, catch=None)

Takes 1 or 2 arguments.

  • If only the argument then is provided, it will be invoked with the resolution value if the promise is resolved

  • If catch is provided, it will be invoked with the rejection value if the promise is rejected

It returns a promise, to allow chaining

catch(catch)

It invokes internally then(None, catch) to register the callable catch for invocation in case the promise is rejected

It returns a promise, to allow chaining