Skip to content

Instantly share code, notes, and snippets.

Created February 4, 2022 17:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
;; An example usage of an imaginary `iteration-async` API, which uses
;; continuation passing to signal when to continue to the next page,
;; completion, and failure
(defn fetch-pages-async
"Fetches summary pages repeatedly until no pages are left.
Calls `done` callback when complete. Calls `err` if an error occurs."
([done err url]
(fetch-summaries done err url 0))
([done err url page]
(fn [cont fail page]
(-> (js/fetch (str url "?page=" page))
(.then #(.json %))
;; call `cont` callback with results to collect and continue fetching
;; the next page.
;; call `fail` callback if the request fails in some way
(.then cont fail)))
:kf :next-page
:vf :data
:initk page
:somef some?
:done done
:error err)))
;; helper fns for when you are working strictly with promises or channels
(defn iteration-promise
[pf & {:keys [kf vf initk somef]}]
(fn [resolve reject]
(iteration-async pf :kf kf :vf vf :initk initk :somef somef :done resolve :error reject))))
(defn iteration-chan
[vchan errchan & {:keys [kf vf somef]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment