With these two macros
(ns guller.macros)
(defmacro then->>
[x & forms]
(loop [x x, forms forms]
(if forms
(let [form (first forms)
threaded (if (seq? form)
`(.then ~x #(~(first form) ~@(next form) %))
`(.then ~x #(~form %)))]
(recur threaded (next forms)))
x)))
(defmacro then->
[x & forms]
(loop [x x, forms forms]
(if forms
(let [form (first forms)
threaded (if (seq? form)
`(.then ~x #(~(first form) % ~@(next form)))
`(.then ~x #(~form %)))]
(recur threaded (next forms)))
x)))
I was able to write the code like this
(ns guller.core
(:require-macros [guller.macros :refer [then-> then->>]]))
(require '[cljs.pprint :as pp :refer [pprint]])
(def github (js/require "github"))
(def githubapi (github.))
(defn flip
"given a function, create a flipped 2-argument function"
[f]
(fn [a b] (f b a)))
(defn getOrgs
"Get the organisations for an authenticated user"
[]
(then-> (.. githubapi -users (getOrgs {}))
(js->clj :keywordize-keys true)
:data
((flip map) :login)))
(defn getReposForOrg
"Get all the repos for an Org"
[org]
(then-> (.. githubapi -repos (getForOrg #js {:org org}))
(js->clj :keywordize-keys true)
:data
((flip map) :ssh_url)
pprint))
(defn -main []
(.. githubapi (authenticate #js {:type "netrc"}))
(then->> (getOrgs)
(mapv getReposForOrg)))
Seems to do a good job of hiding call-backs and promises.
The threading looks familiar.
Not sure if I prefer the reduce
(see history) version or not as gives more control over the position of the response.
Actually it’s not that good; it works over synchronous call backs i.e., doesn’t actually chain promises. Hmmmmm.
Interesting stuff