Created
October 14, 2017 16:30
-
-
Save xmonader/d25def80b9db7ba3aab8bba61f4a0b9b to your computer and use it in GitHub Desktop.
promisesexample.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
users = [] | |
def registerUser(username): | |
global users | |
if username.isdigit(): | |
raise ValueError("Invalid username: {}".format(username)) | |
users.append(username) | |
return username | |
def touchFileForUser(username): | |
print("Creating file for {}".format(username)) | |
def newWelcomeEmail(username): | |
print("Welcome {} to company X .inc".format(username)) | |
p1 = Promise(lambda resolve, reject: resolve(registerUser("ahmed"))) | |
p1.then(lambda u: touchFileForUser(u)) | |
p1.then(lambda u: print("Can we create an email too?")) | |
p2 = Promise(lambda resolve, reject: resolve(registerUser("dmdm"))) | |
p2.then(touchFileForUser) | |
p2.then(newWelcomeEmail) | |
p3 = Promise(lambda resolve, reject: resolve(registerUser("30"))) | |
p3.then(touchFileForUser) | |
p3.catch(lambda msg: print("ERRROR HAPPENED {}".format(msg))) | |
print(p1, p2, p3) | |
print(users) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment