-
Notifications
You must be signed in to change notification settings - Fork 1
/
promise.cljc
45 lines (40 loc) · 1012 Bytes
/
promise.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(ns promisespromises.promise
#?(:cljs (:require-macros [promisespromises.promise]))
(:require
[promesa.core]
[promisespromises.util.macro]))
(defmacro always
"catch any sync exception from evaluating body, and wrap
in an errored promise - allowing a single promise-based
control-flow in promise chains"
[body]
`(promisespromises.util.macro/try-catch
~body
(catch e# (promesa.core/rejected e#))))
(defmacro catch-always
"catch any sync or promise error"
[body handler]
`(promesa.core/catch
(always ~body)
~handler))
(defmacro chain-always
"always chain"
[body handler]
`(promesa.core/chain
(always ~body)
~handler))
(defmacro handle-always
"handly any sync or promise error"
[body handler]
`(promesa.core/handle
(always ~body)
~handler))
(defmacro merge-always
"merge both branches of a promise into a variant"
[p]
`(handle-always
~p
(fn [v# err#]
(if (some? err#)
[::error err#]
[::ok v#]))))