Skip to content

Commit 36e57f9

Browse files
authored
Async promise print + async/await support (#406)
1 parent 169033b commit 36e57f9

7 files changed

Lines changed: 42 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ For a list of breaking changes, check [here](#breaking-changes).
44

55
[Nbb](https://github.com/babashka/nbb): Scripting in Clojure on Node.js using [SCI](https://github.com/babashka/sci)
66

7+
## 1.4.206 (2026-02-07)
8+
9+
- Support async/await. See [docs](https://github.com/babashka/sci/blob/master/doc/async-await.md) for syntax.
10+
- Print promise result value in REPL/nREPL: `(js/Promise.resolve 1) ;;=> #<Promise 1>`
11+
712
## 1.3.205 (2025-12-19)
813

914
- [#395](https://github.com/babashka/nbb/issues/395): fix `vim-fireplace` infinite loop on nREPL session close.

deps.edn

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{:paths ["src"]
22
:aliases {:test {:extra-paths ["test"]}}
3-
:deps {thheller/shadow-cljs {:mvn/version "3.3.4"}
3+
:deps {thheller/shadow-cljs {:mvn/version "3.3.5"}
44
#_#_org.clojure/clojurescript {:mvn/version "1.11.51"}
55
;; reagent/reagent {:mvn/version "1.0.0"}
66
cljsjs/react {:mvn/version "18.2.0-1"}
@@ -14,8 +14,8 @@
1414
cljs-bean/cljs-bean {:mvn/version "1.9.0"}
1515
org.babashka/sci
1616
#_{:local/root "../babashka/sci"}
17-
#_{:mvn/version "0.9.44"}
18-
{:git/url "https://github.com/babashka/sci"
17+
{:mvn/version "0.12.51"}
18+
#_{:git/url "https://github.com/babashka/sci"
1919
:git/sha "33995afb71d58dae270f1a8035e21196ea2157f0"}
2020
org.clojure/tools.cli {:mvn/version "1.0.214"}
2121
com.cognitect/transit-cljs {:mvn/version "0.8.280"}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nbb",
3-
"version": "1.3.205",
3+
"version": "1.4.205",
44
"type": "module",
55
"main": "index.mjs",
66
"bin": {

shadow-cljs.edn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
:target :esm
88
:runtime :node
99
:output-dir "lib"
10+
:devtools {:enabled false}
1011
:modules
1112
{:nbb_core {:init-fn nbb.core/init}
1213
:nbb_goog_string {:init-fn nbb.impl.gstring/init

src/nbb/impl/nrepl_server.cljs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,15 @@
9696
(sci/alter-var-root sci/*3 (constantly @sci/*2))
9797
(sci/alter-var-root sci/*2 (constantly @sci/*1))
9898
(sci/alter-var-root sci/*1 (constantly v))
99-
(let [v (format-value (:nrepl.middleware.print/print request)
100-
(:nrepl.middleware.print/options request)
101-
v)]
102-
(send-fn request {"value" v
103-
"ns" (str sci-ns)}))))
99+
(if (instance? js/Promise v)
100+
(.then v (fn [resolved]
101+
(send-fn request {"value" (str "#<Promise " (pr-str resolved) ">")
102+
"ns" (str sci-ns)})))
103+
(let [v (format-value (:nrepl.middleware.print/print request)
104+
(:nrepl.middleware.print/options request)
105+
v)]
106+
(send-fn request {"value" v
107+
"ns" (str sci-ns)})))))
104108

105109
(defn handle-error [send-fn request e]
106110
(sci/alter-var-root sci/*e (constantly e))
@@ -126,8 +130,8 @@
126130
(.then v
127131
(fn [v]
128132
;; (prn :v v)
129-
(send-value request send-fn v)
130-
(loop-fn v))))))
133+
(-> (js/Promise.resolve (send-value request send-fn v))
134+
(.then #(loop-fn v))))))))
131135
(catch :default e
132136
(handle-error send-fn request e)
133137
(loop-fn nil))))]

src/nbb/impl/repl.cljs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@
7272
(let [ctx #js {:f (if socket (fn [] wrapper)
7373
(fn []
7474
(let [v (first wrapper)]
75-
(prn v)
76-
wrapper)))}
75+
(if (instance? js/Promise v)
76+
(.then v (fn [resolved]
77+
(println (str "#<Promise " (pr-str resolved) ">"))
78+
wrapper))
79+
(do (prn v)
80+
wrapper)))))}
7781
_ (.createContext vm ctx)]
7882
(.runInContext vm "f()" ctx
7983
#js {:displayErrors true
@@ -121,10 +125,13 @@
121125
(sci/alter-var-root sci/*3 (constantly @sci/*2))
122126
(sci/alter-var-root sci/*2 (constantly @sci/*1))
123127
(sci/alter-var-root sci/*1 (constantly val))
124-
(when socket
125-
(.write socket (prn-str val))
126-
#_(prn val))
127-
(continue rl socket))))
128+
(if (and socket (instance? js/Promise val))
129+
(.then val (fn [resolved]
130+
(.write socket (str "#<Promise " (pr-str resolved) ">\n"))
131+
(continue rl socket)))
132+
(do (when socket
133+
(.write socket (prn-str val)))
134+
(continue rl socket))))))
128135
(.catch (fn [err]
129136
(prn (str err))
130137
(sci/alter-var-root sci/*e (constantly err))

test/nbb/test_test.cljs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
(defmethod t/report [:cljs.test/default :end-run-tests] [m])
1717
(t/deftest foo (t/is (= 1 2))) (t/run-tests 'foo0)"))
1818
(.then (fn [_]
19-
(is (str/includes? @output "expected: (= 1 2) actual: (not (= 1 2))")))))))
19+
(is (str/includes? @output "expected: (= 1 2)"))
20+
(is (str/includes? @output "actual: (not (= 1 2))")))))))
2021

2122
(deftest-async cljs-test-test
2223
(let [output (atom "")]
@@ -29,7 +30,8 @@
2930
(t/deftest foo (t/is (= 1 2)))
3031
(cljs.test/deftest bar (t/is (= 1 2))) (t/run-tests 'foo01)"))
3132
(.then (fn [_]
32-
(is (str/includes? @output "expected: (= 1 2) actual: (not (= 1 2))")))))))
33+
(is (str/includes? @output "expected: (= 1 2)"))
34+
(is (str/includes? @output "actual: (not (= 1 2))")))))))
3335

3436
(deftest-async refer-macros-test
3537
(let [output (atom "")]
@@ -41,7 +43,8 @@
4143
(defmethod t/report [:cljs.test/default :end-run-tests] [m])
4244
(deftest foo (is (= 1 2))) (t/run-tests 'foo02)"))
4345
(.then (fn [_]
44-
(is (str/includes? @output "expected: (= 1 2) actual: (not (= 1 2))")))))))
46+
(is (str/includes? @output "expected: (= 1 2)"))
47+
(is (str/includes? @output "actual: (not (= 1 2))")))))))
4548

4649
(deftest-async are-test
4750
(let [output (atom "")]
@@ -54,7 +57,8 @@
5457
(defmethod t/report [:cljs.test/default :end-run-tests] [m])
5558
(t/deftest foo (t/are [x] (= x 2) 1 2 3)) (t/run-tests 'foo-are)")
5659
(.then (fn [_]
57-
(is (str/includes? @output "expected: (= 3 2) actual: (not (= 3 2))"))))
60+
(is (str/includes? @output "expected: (= 3 2)"))
61+
(is (str/includes? @output "actual: (not (= 3 2))"))))
5862
(.then (fn [_]
5963
(reset! output "")
6064
(nbb/load-string "

0 commit comments

Comments
 (0)