Skip to content

Instantly share code, notes, and snippets.

@leonoel
Created October 23, 2023 07:17
Show Gist options
  • Save leonoel/ee46ddf2629da45e75f4e411d18030b7 to your computer and use it in GitHub Desktop.
Save leonoel/ee46ddf2629da45e75f4e411d18030b7 to your computer and use it in GitHub Desktop.
missionary promise interop with abort
(ns promise)
(defmacro with-abort "
Returns a task evaluating `body` with symbol `a` bound to a fresh `AbortSignal` that is aborted when the task is
cancelled. The task completes with the result of returned promise.
" [a & body]
`(fn [s# f#]
(let [c# (js/AbortController.)
~a (.-signal c#)]
(try (.then (do ~@body) s# f#)
(catch :default e# (f# e#)))
#(.abort c#))))
(ns usage
(:require-macros [promise :refer [with-abort]]))
(defn fetch "
Returns a task fetching resource at given `path`.
" [path]
(with-abort abort
(js/fetch path (js-obj "signal" abort))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment