Created
September 18, 2015 14:00
-
-
Save lukemorton/9394edfee88aa01a5d37 to your computer and use it in GitHub Desktop.
core.async versus es7 await/async
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
(defn async-get [url] | |
(let [c (channel)] | |
(ajax/get url {:when-done #(>!! c %)}) | |
c)) | |
(defn sorted-user-repos [user] | |
(sort #(compare ((%1 :stars) (%2 :stars))) (user :repos))) | |
(defn most-popular-repo [username] | |
(let [user (go (<! (async-get "/users.json" {:username username}))) | |
repos (sorted-user-repos user) | |
most-popular (first repos)] | |
(go (<! (async-get "/repo.json" {:username username | |
:repo (most-popular :name)}))))) | |
(.log console (most-popular-repo 'drpheltright')) |
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
async function mostPopularRepo (username) { | |
let user = await getUser(username); | |
let repos = user.repos.sort((a, b) => a.stars < b.stars); | |
let mostPopular = repos[0]; | |
return await getRepo(username, mostPopular.name); | |
} | |
mostPopularRepo('drpheltright') | |
.then(repo => console.log(repo)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment