Skip to content

Commit

Permalink
Fix funcool#112: doseq
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Sep 28, 2022
1 parent ce3dec2 commit 3b9b92f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions .clj-kondo/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:config-paths ["../resources/clj-kondo.exports/funcool/promesa"]}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ pom.xml.asc
/dist
/.cpcache
/settings.xml
/.rebel_readline_history
/.rebel_readline_history.cache
.cache
3 changes: 2 additions & 1 deletion resources/clj-kondo.exports/funcool/promesa/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
promesa.core/plet clojure.core/let
promesa.core/loop clojure.core/loop
promesa.core/recur clojure.core/recur
promesa.core/with-redefs clojure.core/with-redefs}}
promesa.core/with-redefs clojure.core/with-redefs
promesa.core/doseq clojure.core/doseq}}
13 changes: 11 additions & 2 deletions src/promesa/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
(:refer-clojure :exclude [delay spread promise
await map mapcat run!
future let loop recur
-> ->> as-> with-redefs do])
-> ->> as-> with-redefs do
doseq])
(:require
[promesa.protocols :as pt]
[clojure.core :as c]
Expand Down Expand Up @@ -319,7 +320,7 @@
:rejections []})]
(create
(fn [resolve reject]
(doseq [p promises]
(c/doseq [p promises]
(c/-> (pt/-promise p)
(then (fn [v]
(when-not (:resolved @state)
Expand Down Expand Up @@ -586,3 +587,11 @@
(promesa.core/finally
(fn [_# _#]
~@(c/map bind-value resets)))))))

(defmacro doseq
"Simplified version of `doseq` which takes one binding and a seq, and
runs over it using `promesa.core/run!`"
[[binding xs] & body]
`(run! (fn [~binding]
(promesa.core/do ~@body))
~xs))
12 changes: 12 additions & 0 deletions test/promesa/tests/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,22 @@
redefs-var :mocked]
(p/then (redefs-async-fn)
(fn [v]
;; NOTE: this value is unused - should it be removed?
;; Reported by clj-kondo
(not= redefs-async-fn original-fn)
[v redefs-var])))
test #(p/then p1 (fn [res]
(t/is (= res ["mocked" :mocked]))
(t/is (= redefs-async-fn original-fn))))]
#?(:cljs (t/async done (p/do (test) (done)))
:clj @(test))))

(t/deftest doseq-test
(let [test #(p/let [state (atom [])
xs [10 20 30]]
(p/doseq [x xs]
(p/delay (- 100 x))
(swap! state conj x))
(t/is (= xs @state)))]
#?(:cljs (t/async done (p/do (test) (done)))
:clj @(test))))

0 comments on commit 3b9b92f

Please sign in to comment.