Vine: Python Promises

5.1.0 · active · verified Sat Mar 28

Vine is a lightweight Python library that implements 'promises' for managing future evaluations and lazy computations, particularly useful in asynchronous and event-driven programming paradigms. It enhances flexibility by allowing nested promises within callbacks and error handlers. As of version 5.1.0, it is actively maintained by the Celery project.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a promise, attach success and error handlers using `.then()` and `on_error`, and then fulfill the promise with a value or an exception.

from vine import promise

# Create a promise instance
p = promise()

# Attach a callback for successful completion
def on_success(result):
    print(f"Promise fulfilled with: {result}")

p.then(on_success)

# Attach an error handler for failures
def on_error(exc):
    print(f"Promise failed with error: {exc!r}")

p.on_error = on_error

# Fulfill the promise with a value
print("Fulfilling promise with 100...")
p(100)
# Expected output: Promise fulfilled with: 100

# Create another promise to demonstrate error
p_error = promise()
p_error.then(on_success) # Attach same success handler
p_error.on_error = on_error # Attach same error handler

# Fulfill the promise with an error
print("\nFulfilling promise with an exception...")
p_error.throw(ValueError("Operation failed!"))
# Expected output: Promise failed with error: ValueError('Operation failed!')

view raw JSON →