GithubHelp home page GithubHelp logo

promises's Introduction

Promises

A Promise is a way of giving functions callbacks to run, depending on whether they succeed or fail.

This implementation modifies this slightly -- instead of a single Success or Failure return type, it provides an "on" method to specify what the return value should be.

@promise
def add_one(n):
    return n + 1

def success(n):
    print("This function will be called with n: {}".format(n))

def failure(n):
    assert False, "This function won't be called!"

(
    add_one(12)
        .on(13, success, failure)
        .go()
)

Promises run asynchronously by default: the .go() method doesn't block.

Later on, if you need to retrieve the result of the success() or failure() method called last, you can run .wait() on the promise.

my_promise = add_one(5).go()
print("5+1 is being calculated in the background")
result = my_promise.wait()
assert result == 6

(You can also run .wait() straight away, and the .go() method will be called implicitly.)

result = (
    add_one(5)
        .on(6, add_one, failure)
        .wait()
)

assert result == 7

If you've decorated some function with the @promise decorator, but don't want to use it as a promise, you can pass the no_promise keyword argument in like so:

result = add_one(5, no_promise=True)
assert result == 6

Some more example code can be found in example.py

promises's People

Contributors

bedekelly avatar

Stargazers

Aran Long avatar

Watchers

Aran Long avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.