Skip to content

Commit

Permalink
Implement basic with-retries functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
verberktstan committed Feb 22, 2024
1 parent 2461b02 commit 7efd1b9
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/swark/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@
(apply f args)
#?(:cljs (catch :default _ nil) :clj (catch Throwable _ nil))))

(defn with-retries
{:added "0.1.41" ; NOTE: To be released!
:arglist '([n f & args])
:doc "Returns the result of (apply f args) after running it n times. When
something is thrown on the last try, returns the throwable map."}
[n f & args]
(-> n pos-int? assert)
(loop [n n, result nil]
(if (zero? n)
(or (jab Throwable->map result) result) ; Try to coerce to map if something is Thrown
(recur
(dec n)
(try
(apply f args)
(catch Throwable t t))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Regarding strings

Expand Down

0 comments on commit 7efd1b9

Please sign in to comment.