code-reviews

2020-07-05T21:16:58.139100Z

Wrote a quick macro, would love your thoughts!

;; fut-bg
;; future only throw once derefed
;; this macro lets you run futures in the background, without
;; ever dereferencing them, by wrapping a top-level try-catch
(defmacro fut-bg [& form]
  `(future
     (try
       ~@form
       (catch Exception e#
         (log/errorf "uh-oh, failed to run async function %s %s" '~form e#)))))
Mainly: • Is there another way to do this? perhaps this macro already exists • Would you do it differently?

phronmophobic 2020-07-05T21:18:45.139600Z

i would also consider rethrowing the the exception in case someone does deref it

phronmophobic 2020-07-05T21:19:11.140100Z

(throw e#) as the last statement of the catch expression

2020-07-05T21:19:28.140300Z

oo nice!

seancorfield 2020-07-05T21:46:46.141500Z

Timbre has a logged-future that is basically this. We have the same macro in one of our "utility" namespaces at work. We don't rethrow the exception after logging it, but that's probably a good idea.

❤️ 2
2020-07-05T22:40:45.142600Z

awesome, thanks for the context team!