Skip to content

Instantly share code, notes, and snippets.

@lilactown
Created February 4, 2022 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lilactown/e78f4f24d9f6e282d5c6940772ed179a to your computer and use it in GitHub Desktop.
Save lilactown/e78f4f24d9f6e282d5c6940772ed179a to your computer and use it in GitHub Desktop.
;; 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]
(iteration-async
(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]}]
(js/Promise.
(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