(<cb! ?exp)
Like <!
for callback based functions via cb->c
convertion.
Like `<!` for callback based functions via `cb->c` convertion.
(cb->c exp)
(cb->c [f & forms :as _exp] auto-close?)
Creates a chan
base on the callbacks of exp
. Symbols
callback
/ resolve
and reject
marks the callback
position of exp
to put resolutions and rejections onto
the new created channel. If no mark is given, assumes resolve
position is the last arg of exp
and will append it.
exp
callbacks must expect only one argument!
Rejections will be used as cause
for a new created ExceptionInfo
.
Given auto-close?
false
the caller of cb->c
is responsible for
closing the channel! Otherwise channel will be closed after first put
(resolve or reject).
If calling exp
fails an error / exception will be put onto the
new created channel with fail information. The channel will be
closed then.
Example:
(let [callback-based-fn
(fn callback-based-fn
[value-to-carry has-to-fail? success fail]
(Thread/sleep 1000)
(if has-to-fail?
(fail [:nope value-to-carry])
(success [:yeah value-to-carry])))]
(go
(try
(-> (callback-based-fn
5 true
resolve
;; fn must be inline so that `cb->c` can recognize `reject` mark!
(fn modifiy-error-before-reject-it [error]
(->> {:error error}
(ex-info "nix-gut")
(reject))))
(<cb!)
(println))
(catch ExceptionInfo e
(println e)))))
Creates a `chan` base on the callbacks of `exp`. Symbols `callback` / `resolve` and `reject` marks the callback position of `exp` to put resolutions and rejections onto the new created channel. If no mark is given, assumes `resolve` position is the last arg of `exp` and will append it. `exp` callbacks must expect only one argument! Rejections will be used as `cause` for a new created `ExceptionInfo`. Given `auto-close?` `false` the caller of `cb->c` is responsible for closing the channel! Otherwise channel will be closed after first put (resolve or reject). If calling `exp` fails an error / exception will be put onto the new created channel with fail information. The channel will be closed then. Example: ``` (let [callback-based-fn (fn callback-based-fn [value-to-carry has-to-fail? success fail] (Thread/sleep 1000) (if has-to-fail? (fail [:nope value-to-carry]) (success [:yeah value-to-carry])))] (go (try (-> (callback-based-fn 5 true resolve ;; fn must be inline so that `cb->c` can recognize `reject` mark! (fn modifiy-error-before-reject-it [error] (->> {:error error} (ex-info "nix-gut") (reject)))) (<cb!) (println)) (catch ExceptionInfo e (println e))))) ```
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close