Created
October 23, 2023 07:17
-
-
Save leonoel/ee46ddf2629da45e75f4e411d18030b7 to your computer and use it in GitHub Desktop.
missionary promise interop with abort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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#)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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