{"action":"created","comment":{"url":"https://api.github.com/repos/khanlou/Promise/pulls/comments/78877407","id":78877407,"diff_hunk":"@@ -0,0 +1,155 @@\n+\n+import Promise  // <-- If there is an error here, build the project first.\n+\n+/*\n+ \n+ Playing with the ideas in http://khanlou.com/2016/08/common-patterns-with-promises/\n+ \n+ Set each `if false` to `true` to run that example.\n+\n+ */\n+\n+\n+\n+/// Helper function to build/fulfill a Promise.\n+/// - returns: A Promise that fulfills with the given string, after a delay.\n+func promisedString(str: String) -> Promise<String> {\n+    return Promise<String>(work: { fulfill, reject in\n+        sleep(1)\n+        fulfill(str)\n+    })\n+}\n+\n+\n+\n+// - - - - - - - - - - - - - - - - - - - - - - - - - - -\n+// Using .all()\n+\n+if false {\n+    print(\"Running .all() example\")\n+\n+    let strings = [ \"a\", \"b\", \"c\" ]\n+    let promises = strings.map(promisedString)\n+\n+    Promise<[String]>.all(promises).then { allStrings -> Void in\n+        print(\"got em all:\", allStrings)\n+    }\n+}\n+\n+\n+\n+// - - - - - - - - - - - - - - - - - - - - - - - - - - -\n+// Using .delay()\n+\n+if false {\n+    print(\"Running .delay() example\")\n+\n+    Promise<Void>.delay(1.0).then { _ -> Void in\n+        print(\"after delay\")\n+    }\n+}\n+\n+\n+// - - - - - - - - - - - - - - - - - - - - - - - - - - -\n+// Race\n+\n+if false {\n+    print(\"Running .race() example\")\n+\n+    let strings = [ \"a\", \"b\", \"c\", \"d\", \"e\", \"f\" ]\n+    let promises = strings.map(promisedString)\n+\n+    Promise<String>.race(promises).then { (value) -> Void in\n+        // If you run this multiple times, you might get a different result each time\n+        print(\"got: \\(value)\")\n+    }\n+}\n+\n+\n+\n+// - - - - - - - - - - - - - - - - - - - - - - - - - - -\n+// Timeout\n+\n+if false {\n+    print(\"Running .timeout() example\")\n+\n+    promisedString(\"timeout\")\n+        .addTimeout(0.5)  // NOTE: 0.5 will fail, 1.5 will succeed\n+        .then({ _ -> Void in\n+            print(\"success from timeout\")\n+        })\n+        .onFailure { error in\n+            print(\"failure from timeout: \\(error)\")\n+    }\n+}\n+\n+\n+\n+// - - - - - - - - - - - - - - - - - - - - - - - - - - -\n+// Recover\n+\n+/// Helper function to make a promise that succeeds or fails from the given value\n+func failablePromise(str: String, fail: Bool) -> Promise<String> {\n+    let promise = Promise<String>()\n+    dispatch_async(dispatch_get_global_queue(0, 0)) {\n+        if fail {\n+            promise.reject(NSError(domain: \"rejected\", code: 1, userInfo: nil))\n+        }\n+        else {\n+            promise.fulfill(str)\n+        }\n+    }\n+    return promise\n+}","path":"Promise.playground/Pages/Common Patterns.xcplaygroundpage/Contents.swift","position":103,"original_position":103,"commit_id":"15f725340d0f93d7e3d504e987a6663b3db0d757","original_commit_id":"15f725340d0f93d7e3d504e987a6663b3db0d757","user":{"login":"kelan","id":13314,"avatar_url":"https://avatars.githubusercontent.com/u/13314?v=3","gravatar_id":"","url":"https://api.github.com/users/kelan","html_url":"https://github.com/kelan","followers_url":"https://api.github.com/users/kelan/followers","following_url":"https://api.github.com/users/kelan/following{/other_user}","gists_url":"https://api.github.com/users/kelan/gists{/gist_id}","starred_url":"https://api.github.com/users/kelan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kelan/subscriptions","organizations_url":"https://api.github.com/users/kelan/orgs","repos_url":"https://api.github.com/users/kelan/repos","events_url":"https://api.github.com/users/kelan/events{/privacy}","received_events_url":"https://api.github.com/users/kelan/received_events","type":"User","site_admin":false},"body":"> also you can use the work: initializer\r\nOh, right.","created_at":"2016-09-15T01:51:49Z","updated_at":"2016-09-15T01:51:49Z","html_url":"https://github.com/khanlou/Promise/pull/2#discussion_r78877407","pull_request_url":"https://api.github.com/repos/khanlou/Promise/pulls/2","_links":{"self":{"href":"https://api.github.com/repos/khanlou/Promise/pulls/comments/78877407"},"html":{"href":"https://github.com/khanlou/Promise/pull/2#discussion_r78877407"},"pull_request":{"href":"https://api.github.com/repos/khanlou/Promise/pulls/2"}}},"pull_request":{"url":"https://api.github.com/repos/khanlou/Promise/pulls/2","id":84764803,"html_url":"https://github.com/khanlou/Promise/pull/2","diff_url":"https://github.com/khanlou/Promise/pull/2.diff","patch_url":"https://github.com/khanlou/Promise/pull/2.patch","issue_url":"https://api.github.com/repos/khanlou/Promise/issues/2","number":2,"state":"open","locked":false,"title":"Add a Playground so you can play with Promises interactively","user":{"login":"kelan","id":13314,"avatar_url":"https://avatars.githubusercontent.com/u/13314?v=3","gravatar_id":"","url":"https://api.github.com/users/kelan","html_url":"https://github.com/kelan","followers_url":"https://api.github.com/users/kelan/followers","following_url":"https://api.github.com/users/kelan/following{/other_user}","gists_url":"https://api.github.com/users/kelan/gists{/gist_id}","starred_url":"https://api.github.com/users/kelan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kelan/subscriptions","organizations_url":"https://api.github.com/users/kelan/orgs","repos_url":"https://api.github.com/users/kelan/repos","events_url":"https://api.github.com/users/kelan/events{/privacy}","received_events_url":"https://api.github.com/users/kelan/received_events","type":"User","site_admin":false},"body":"Changes to the Promise code:\r\n* Add `public` to everything so it's accessible from outside the module.\r\n* Add some headerdoc (mostly copy/pasted from http://khanlou.com/2016/08/common-patterns-with-promises/)","created_at":"2016-09-10T19:07:59Z","updated_at":"2016-09-15T01:51:49Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3d8b4d52c59680d89df5de4e92a7000afcdbbb78","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/khanlou/Promise/pulls/2/commits","review_comments_url":"https://api.github.com/repos/khanlou/Promise/pulls/2/comments","review_comment_url":"https://api.github.com/repos/khanlou/Promise/pulls/comments{/number}","comments_url":"https://api.github.com/repos/khanlou/Promise/issues/2/comments","statuses_url":"https://api.github.com/repos/khanlou/Promise/statuses/15f725340d0f93d7e3d504e987a6663b3db0d757","head":{"label":"kelan:playground","ref":"playground","sha":"15f725340d0f93d7e3d504e987a6663b3db0d757","user":{"login":"kelan","id":13314,"avatar_url":"https://avatars.githubusercontent.com/u/13314?v=3","gravatar_id":"","url":"https://api.github.com/users/kelan","html_url":"https://github.com/kelan","followers_url":"https://api.github.com/users/kelan/followers","following_url":"https://api.github.com/users/kelan/following{/other_user}","gists_url":"https://api.github.com/users/kelan/gists{/gist_id}","starred_url":"https://api.github.com/users/kelan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kelan/subscriptions","organizations_url":"https://api.github.com/users/kelan/orgs","repos_url":"https://api.github.com/users/kelan/repos","events_url":"https://api.github.com/users/kelan/events{/privacy}","received_events_url":"https://api.github.com/users/kelan/received_events","type":"User","site_admin":false},"repo":{"id":67888237,"name":"Promise","full_name":"kelan/Promise","owner":{"login":"kelan","id":13314,"avatar_url":"https://avatars.githubusercontent.com/u/13314?v=3","gravatar_id":"","url":"https://api.github.com/users/kelan","html_url":"https://github.com/kelan","followers_url":"https://api.github.com/users/kelan/followers","following_url":"https://api.github.com/users/kelan/following{/other_user}","gists_url":"https://api.github.com/users/kelan/gists{/gist_id}","starred_url":"https://api.github.com/users/kelan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kelan/subscriptions","organizations_url":"https://api.github.com/users/kelan/orgs","repos_url":"https://api.github.com/users/kelan/repos","events_url":"https://api.github.com/users/kelan/events{/privacy}","received_events_url":"https://api.github.com/users/kelan/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/kelan/Promise","description":"A Promise library for Swift, based partially on Javascript's A+ spec","fork":true,"url":"https://api.github.com/repos/kelan/Promise","forks_url":"https://api.github.com/repos/kelan/Promise/forks","keys_url":"https://api.github.com/repos/kelan/Promise/keys{/key_id}","collaborators_url":"https://api.github.com/repos/kelan/Promise/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/kelan/Promise/teams","hooks_url":"https://api.github.com/repos/kelan/Promise/hooks","issue_events_url":"https://api.github.com/repos/kelan/Promise/issues/events{/number}","events_url":"https://api.github.com/repos/kelan/Promise/events","assignees_url":"https://api.github.com/repos/kelan/Promise/assignees{/user}","branches_url":"https://api.github.com/repos/kelan/Promise/branches{/branch}","tags_url":"https://api.github.com/repos/kelan/Promise/tags","blobs_url":"https://api.github.com/repos/kelan/Promise/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/kelan/Promise/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/kelan/Promise/git/refs{/sha}","trees_url":"https://api.github.com/repos/kelan/Promise/git/trees{/sha}","statuses_url":"https://api.github.com/repos/kelan/Promise/statuses/{sha}","languages_url":"https://api.github.com/repos/kelan/Promise/languages","stargazers_url":"https://api.github.com/repos/kelan/Promise/stargazers","contributors_url":"https://api.github.com/repos/kelan/Promise/contributors","subscribers_url":"https://api.github.com/repos/kelan/Promise/subscribers","subscription_url":"https://api.github.com/repos/kelan/Promise/subscription","commits_url":"https://api.github.com/repos/kelan/Promise/commits{/sha}","git_commits_url":"https://api.github.com/repos/kelan/Promise/git/commits{/sha}","comments_url":"https://api.github.com/repos/kelan/Promise/comments{/number}","issue_comment_url":"https://api.github.com/repos/kelan/Promise/issues/comments{/number}","contents_url":"https://api.github.com/repos/kelan/Promise/contents/{+path}","compare_url":"https://api.github.com/repos/kelan/Promise/compare/{base}...{head}","merges_url":"https://api.github.com/repos/kelan/Promise/merges","archive_url":"https://api.github.com/repos/kelan/Promise/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/kelan/Promise/downloads","issues_url":"https://api.github.com/repos/kelan/Promise/issues{/number}","pulls_url":"https://api.github.com/repos/kelan/Promise/pulls{/number}","milestones_url":"https://api.github.com/repos/kelan/Promise/milestones{/number}","notifications_url":"https://api.github.com/repos/kelan/Promise/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/kelan/Promise/labels{/name}","releases_url":"https://api.github.com/repos/kelan/Promise/releases{/id}","deployments_url":"https://api.github.com/repos/kelan/Promise/deployments","created_at":"2016-09-10T18:42:22Z","updated_at":"2016-09-10T18:42:23Z","pushed_at":"2016-09-10T19:37:58Z","git_url":"git://github.com/kelan/Promise.git","ssh_url":"git@github.com:kelan/Promise.git","clone_url":"https://github.com/kelan/Promise.git","svn_url":"https://github.com/kelan/Promise","homepage":null,"size":40,"stargazers_count":0,"watchers_count":0,"language":"Swift","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"khanlou:master","ref":"master","sha":"f095e5f05417833fc82cfa747fa4b373d967febc","user":{"login":"khanlou","id":850492,"avatar_url":"https://avatars.githubusercontent.com/u/850492?v=3","gravatar_id":"","url":"https://api.github.com/users/khanlou","html_url":"https://github.com/khanlou","followers_url":"https://api.github.com/users/khanlou/followers","following_url":"https://api.github.com/users/khanlou/following{/other_user}","gists_url":"https://api.github.com/users/khanlou/gists{/gist_id}","starred_url":"https://api.github.com/users/khanlou/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/khanlou/subscriptions","organizations_url":"https://api.github.com/users/khanlou/orgs","repos_url":"https://api.github.com/users/khanlou/repos","events_url":"https://api.github.com/users/khanlou/events{/privacy}","received_events_url":"https://api.github.com/users/khanlou/received_events","type":"User","site_admin":false},"repo":{"id":64682247,"name":"Promise","full_name":"khanlou/Promise","owner":{"login":"khanlou","id":850492,"avatar_url":"https://avatars.githubusercontent.com/u/850492?v=3","gravatar_id":"","url":"https://api.github.com/users/khanlou","html_url":"https://github.com/khanlou","followers_url":"https://api.github.com/users/khanlou/followers","following_url":"https://api.github.com/users/khanlou/following{/other_user}","gists_url":"https://api.github.com/users/khanlou/gists{/gist_id}","starred_url":"https://api.github.com/users/khanlou/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/khanlou/subscriptions","organizations_url":"https://api.github.com/users/khanlou/orgs","repos_url":"https://api.github.com/users/khanlou/repos","events_url":"https://api.github.com/users/khanlou/events{/privacy}","received_events_url":"https://api.github.com/users/khanlou/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/khanlou/Promise","description":"A Promise library for Swift, based partially on Javascript's A+ spec","fork":false,"url":"https://api.github.com/repos/khanlou/Promise","forks_url":"https://api.github.com/repos/khanlou/Promise/forks","keys_url":"https://api.github.com/repos/khanlou/Promise/keys{/key_id}","collaborators_url":"https://api.github.com/repos/khanlou/Promise/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/khanlou/Promise/teams","hooks_url":"https://api.github.com/repos/khanlou/Promise/hooks","issue_events_url":"https://api.github.com/repos/khanlou/Promise/issues/events{/number}","events_url":"https://api.github.com/repos/khanlou/Promise/events","assignees_url":"https://api.github.com/repos/khanlou/Promise/assignees{/user}","branches_url":"https://api.github.com/repos/khanlou/Promise/branches{/branch}","tags_url":"https://api.github.com/repos/khanlou/Promise/tags","blobs_url":"https://api.github.com/repos/khanlou/Promise/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/khanlou/Promise/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/khanlou/Promise/git/refs{/sha}","trees_url":"https://api.github.com/repos/khanlou/Promise/git/trees{/sha}","statuses_url":"https://api.github.com/repos/khanlou/Promise/statuses/{sha}","languages_url":"https://api.github.com/repos/khanlou/Promise/languages","stargazers_url":"https://api.github.com/repos/khanlou/Promise/stargazers","contributors_url":"https://api.github.com/repos/khanlou/Promise/contributors","subscribers_url":"https://api.github.com/repos/khanlou/Promise/subscribers","subscription_url":"https://api.github.com/repos/khanlou/Promise/subscription","commits_url":"https://api.github.com/repos/khanlou/Promise/commits{/sha}","git_commits_url":"https://api.github.com/repos/khanlou/Promise/git/commits{/sha}","comments_url":"https://api.github.com/repos/khanlou/Promise/comments{/number}","issue_comment_url":"https://api.github.com/repos/khanlou/Promise/issues/comments{/number}","contents_url":"https://api.github.com/repos/khanlou/Promise/contents/{+path}","compare_url":"https://api.github.com/repos/khanlou/Promise/compare/{base}...{head}","merges_url":"https://api.github.com/repos/khanlou/Promise/merges","archive_url":"https://api.github.com/repos/khanlou/Promise/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/khanlou/Promise/downloads","issues_url":"https://api.github.com/repos/khanlou/Promise/issues{/number}","pulls_url":"https://api.github.com/repos/khanlou/Promise/pulls{/number}","milestones_url":"https://api.github.com/repos/khanlou/Promise/milestones{/number}","notifications_url":"https://api.github.com/repos/khanlou/Promise/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/khanlou/Promise/labels{/name}","releases_url":"https://api.github.com/repos/khanlou/Promise/releases{/id}","deployments_url":"https://api.github.com/repos/khanlou/Promise/deployments","created_at":"2016-08-01T16:13:06Z","updated_at":"2016-09-12T07:38:39Z","pushed_at":"2016-09-15T00:08:22Z","git_url":"git://github.com/khanlou/Promise.git","ssh_url":"git@github.com:khanlou/Promise.git","clone_url":"https://github.com/khanlou/Promise.git","svn_url":"https://github.com/khanlou/Promise","homepage":null,"size":45,"stargazers_count":91,"watchers_count":91,"language":"Swift","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":10,"mirror_url":null,"open_issues_count":3,"forks":10,"open_issues":3,"watchers":91,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/khanlou/Promise/pulls/2"},"html":{"href":"https://github.com/khanlou/Promise/pull/2"},"issue":{"href":"https://api.github.com/repos/khanlou/Promise/issues/2"},"comments":{"href":"https://api.github.com/repos/khanlou/Promise/issues/2/comments"},"review_comments":{"href":"https://api.github.com/repos/khanlou/Promise/pulls/2/comments"},"review_comment":{"href":"https://api.github.com/repos/khanlou/Promise/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/khanlou/Promise/pulls/2/commits"},"statuses":{"href":"https://api.github.com/repos/khanlou/Promise/statuses/15f725340d0f93d7e3d504e987a6663b3db0d757"}}}}
{"actor":{"display_login":"kelan"}}