kubikaugustyn/KUtil

Add Promises

Closed this issue · 1 comments

Promises are in JavaScript, so why not in Python?

from kutil.promise import Promise, await, async

def work(resolve, reject):
    failed = do_stuff()
    if failed:
        reject()
    else:
        resolve()

Promise(work).then(print).catch(lambda x: print("Failed", x))

# OR
@async
def work():
    failed = do_stuff()
    if failed:
        raise ValueError
    # Return is the .then() argument
work().then(...).catch(...)

Async already exists in Python + the asyncio library can help