# vine.promises

Promise implementation.

class vine.promises.promise(fun=None, args=None, kwargs=None, callback=None, on_error=None, weak=False, ignore_result=False)

  Promise of future evaluation.

  This is a special implementation of promises in that it can be used both for “promise of a value” and lazy evaluation. The biggest upside for this is that everything in a promise can also be a promise, e.g. filters, callbacks and errbacks can all be promises.

  Usage examples:

      >>> p = promise()
      >>> p.then(promise(print, ('OK',)))  # noqa
      >>> p.on_error = promise(print, ('ERROR',))  # noqa
      >>> p(20)
      OK, 20
      >>> p.then(promise(print, ('hello',)))  # noqa
      hello, 20

      >>> p.throw(KeyError('foo'))
      ERROR, KeyError('foo')

      >>> p2 = promise()
      >>> p2.then(print)  # noqa
      >>> p2.cancel()
      >>> p(30)

  Example: .. code-block:: python

  > from vine import promise, wrap

  > class Protocol:

  > > def __init__(self):

  > >   self.buffer = []

  > > def receive_message(self):
  > >   return self.read_header().then(
  > >     self.read_body).then(

  > >       wrap(self.prepare_body))

  > > def read(self, size, callback=None):

  > >   callback = callback or promise() tell_eventloop_to_read(size, callback) return callback

  > > def read_header(self, callback=None):

  > >   return self.read(4, callback)

  > > def read_body(self, header, callback=None):

  > >   body_size, = unpack(‘>L’, header) return self.read(body_size, callback)

  > > def prepare_body(self, value):

  > >   self.buffer.append(value)

  args
  cancel()
  cancelled
  failed
  fun
  ignore_result
  kwargs
  property listeners
  on_error
  ready
  reason
  then(callback, on_error=None)
  throw(exc=None, tb=None, propagate=True)
  throw1(exc=None)
  value
  weak