Skip to content

Commit 5334d7b

Browse files
committedMay 5, 2022
Fixes promise-chan put nil and close issues
1 parent 7354995 commit 5334d7b

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed
 

‎project.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject jtk-dvlp/core.async-helpers "3.0.1"
1+
(defproject jtk-dvlp/core.async-helpers "3.0.1-SNAPSHOT"
22
:description
33
"Helper pack for core.async"
44

‎src/jtk_dvlp/async/interop/promise.cljc

+32-23
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@
2222
(let [c (async/promise-chan)
2323

2424
put-val!
25-
(fn [val]
26-
(if (nil? val)
27-
(async/close! c)
28-
(async/put! c val)))
25+
(fn [v]
26+
(when (some? v)
27+
(async/put! c v))
28+
(async/close! c))
2929

3030
forward-error!
31-
(fn [err]
32-
(->> err
31+
(fn [e]
32+
(->> e
3333
(ex-info "promise error" {:code :promise-error})
34-
(async/put! c)))]
34+
(async/put! c))
35+
(async/close! c))]
3536

3637
#?(:clj
3738
(try
@@ -60,12 +61,13 @@
6061
[c]
6162
(create-promise
6263
(fn [resolve reject]
63-
(->> (fn [v]
64-
(async/close! c)
65-
(if (jtk-dvlp.async/exception? v)
66-
(reject v)
67-
(resolve v)))
68-
(async/take! c)))))
64+
(async/take!
65+
c
66+
(fn [v]
67+
(async/close! c)
68+
(if (jtk-dvlp.async/exception? v)
69+
(reject v)
70+
(resolve v)))))))
6971

7072
(defn promise-chan
7173
"Creates an promise like channel, see `core.async/promise-chan`.
@@ -78,18 +80,23 @@
7880
(async/promise-chan))
7981

8082
([f]
81-
(let [c (async/promise-chan)
83+
(let [p (async/promise-chan)
8284

8385
put-resolution!
84-
(partial async/put! c)
86+
(fn [v]
87+
(when (some? v)
88+
(async/put! p v))
89+
(async/close! p))
8590

8691
put-rejection!
87-
#(->> %
88-
(ex-info "promise error" {:code :promise-error})
89-
(async/put! c))]
92+
(fn [e]
93+
(->> e
94+
(ex-info "promise error" {:code :promise-error})
95+
(async/put! p))
96+
(async/close! p))]
9097

9198
(f put-resolution! put-rejection!)
92-
c)))
99+
p)))
93100

94101
(defn ->promise-chan
95102
"Ensure given channel `c` to be a `promise-chan`.
@@ -98,10 +105,12 @@
98105
[c]
99106
(let [p (async/promise-chan)]
100107
(async/take!
101-
c (fn [v]
102-
(when (some? v)
103-
(async/put! p v))
104-
(async/close! p)))
108+
c
109+
(fn [v]
110+
(async/close! c)
111+
(when (some? v)
112+
(async/put! p v))
113+
(async/close! p)))
105114
p))
106115

107116
#?(:clj

0 commit comments

Comments
 (0)