Add a Playground so you can play with Promises interactively #2

Merged
merged 3 commits into from Sep 15, 2016

Projects

None yet

2 participants

@kelan
Contributor
kelan commented Sep 10, 2016

Changes to the Promise code:

@kelan kelan Add a Playground so you can play with Promises interactively
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/)
6a56cec
@kelan
Contributor
kelan commented Sep 10, 2016

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.

@kelan kelan referenced this pull request Sep 10, 2016
Closed

Convert to Swift 3 #3

@kelan kelan Update README to mention the playground
Also add syntax highlighting to the code blocks, and a link to the A+ spec.
15f7253
+
+promisedString("simple example").then { result in
+ print("Got result: ", result)
+}
@khanlou
khanlou Sep 14, 2016 Owner

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)
+ }
@khanlou
khanlou Sep 14, 2016 Owner

trailing block syntax

+
+ Promise<Void>.delay(1.0).then { _ -> Void in
+ print("after delay")
+ }
@khanlou
khanlou Sep 14, 2016 Owner

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)")
+ }
@khanlou
khanlou Sep 14, 2016 Owner

trailing block syntax

+ }
+ }
+ return promise
+}
@khanlou
khanlou Sep 14, 2016 Owner

trailing block syntax, and also you can use the work: initializer instead of dispatch_async

@kelan
kelan Sep 15, 2016 edited Contributor

also you can use the work: initializer

Oh, right, duh.

+ print("succeeded: \(result)")
+ }).onFailure { error in
+ print("failed \(error)")
+ }
@khanlou
khanlou Sep 14, 2016 Owner

trailing block syntax

+ print("succeeded: \(result)")
+ }).onFailure { error in
+ print("failed \(error)")
+ }
@khanlou
khanlou Sep 14, 2016 Owner

trailing block syntax

+ print("got result:", result)
+ }).onFailure { error in
+ print("failed: \(error)")
+ }
@khanlou
khanlou Sep 14, 2016 Owner

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]> {
@khanlou
khanlou Sep 14, 2016 Owner

are these comments formatted such that they show up when the user option-clicks?

@kelan
kelan Sep 15, 2016 edited Contributor

Yeah, it's standard headerdoc for Swift code, and shows up when you option-click on the symbol in Xcode.

screen shot 2016-09-14 at 6 54 35 pm

@kelan
Contributor
kelan commented Sep 15, 2016

Thanks for the feedback! I'll try to work on the style tweaks later tonight.

@kelan
Contributor
kelan commented Sep 15, 2016

I think I got 'em all.

@khanlou khanlou merged commit 84393e2 into khanlou:master Sep 15, 2016
@kelan kelan deleted the kelan:playground branch Sep 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment