New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a Playground so you can play with Promises interactively #2
Conversation
Changes to the Promise code: * Add `public` to everything so it's accessible from outside the module. * Add some headerdoc (mostly copy/pasted from http://khanlou.com/2016/08/common-patterns-with-promises/)
I've been playing with this project recently in a Playground, and thought it was a good example for including the playground with the project, as I talk about here. |
Also add syntax highlighting to the code blocks, and a link to the A+ spec.
|
||
promisedString("simple example").then { result in | ||
print("Got result: ", result) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i know i haven't published a style guide yet, but i'm not using trailing block syntax for this project. i'll comment each time it needs to be fixed
|
||
Promise<[String]>.all(promises).then { allStrings -> Void in | ||
print("got em all:", allStrings) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trailing block syntax
|
||
Promise<Void>.delay(1.0).then { _ -> Void in | ||
print("after delay") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trailing block syntax
Promise<String>.race(promises).then { (value) -> Void in | ||
// If you run this multiple times, you might get a different result each time | ||
print("got: \(value)") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trailing block syntax
print("succeeded: \(result)") | ||
}).onFailure { error in | ||
print("failed \(error)") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trailing block syntax
} | ||
} | ||
return promise | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trailing block syntax, and also you can use the work:
initializer instead of dispatch_async
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also you can use the work: initializer
Oh, right, duh.
print("got result:", result) | ||
}).onFailure { error in | ||
print("failed: \(error)") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trailing block syntax
print("succeeded: \(result)") | ||
}).onFailure { error in | ||
print("failed \(error)") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trailing block syntax
|
||
/// Wait for all the promises you give it to fulfill, and once they have, fulfill itself | ||
/// with the array of all fulfilled values. | ||
public static func all<T>(promises: [Promise<T>]) -> Promise<[T]> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are these comments formatted such that they show up when the user option-clicks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sweet!
Thanks for the feedback! I'll try to work on the style tweaks later tonight. |
I think I got 'em all. |
Changes to the Promise code:
public
to everything so it's accessible from outside the module.