I'm having a hard time with async/await and promesa. Somehow, all combinations don't do what I think they do:
(defn start-server []
(set! the-server (js/Deno.serve routefn)))
(defn stop-server [] (.shutdown the-server))
(defn restart []
(await
(p/-> (stop-server)
(start-server))))
Is there anywhere else than the promesa doku (which seems to be a superset of what is ported to nbb) to read up on this? I've tried several combinations of p/do! with and without await above, none of which seem to wait for the shutdown to finish and then run the server again.are you actually invoking one of these functions?
yes, from the REPL
yes, the promesa docs are the primary source of information on how to use promesa
what are you trying to do in words, perhaps I'm able to give some guidance
.shutdown is an async fn, I'm trying to wait for it to finish and then launch start-server
this is for the restart command
I'd like the repl to block until the server is up again as well
The way to do it should be this:
(require '[nbb.core :refer [await]]
'[promesa.core :as p])
(def the-server nil)
(defn start-server []
(set! the-server (js/Deno.serve (fn [m] (js/Response. "hello")))))
(defn stop-server []
(when the-server (.shutdown the-server)))
(defn restart []
(await
(p/do! (stop-server)
(start-server))))
(restart)I do notice it doesn't block in the nREPL
so perhaps something broke there, but otherwise it should be what you want]
or maybe it does block but it goes very quickly
yeah I do think it works since I'm getting not a promise but a direct JS object back
user=> (require '[nbb.core :refer [await]]
#_=> '[promesa.core :as p])
nil
user=> (def the-server nil)
#'user/the-server
user=>
user=> (defn start-server []
#_=> (set! the-server (js/Deno.serve (fn [m] (js/Response. "hello")))))
#'user/start-server
user=>
user=> (defn stop-server []
#_=> (when the-server (.shutdown the-server)))
#'user/stop-server
user=>
user=> (defn restart []
#_=> (await
#_=> (p/do! (stop-server)
#_=> (start-server))))
#'user/restart
user=> (restart)
#js {:addr #object[Object], :finished #<js/Promise[~]>, :shutdown #object[shutdown], :ref #object[ref], :unref #object[unref]}thanks a lot for the advice, will try this. Which await is used if I don't refer from nbb.core?
a wrong await for sure, I don't know which one :)
well, perhaps it works in the user namespace or so, not sure:
user=> (= await nbb.core/await)
trueuser=> (ns foo)
#object[pt foo]
foo=> (= await nbb.core/await)
Could not resolve symbol: awaitprobably some special cases here for the REPL: https://github.com/babashka/nbb/blob/9119dbe9b5f660cb77d29058b3b762978ee4b939/src/nbb/impl/nrepl_server.cljs#L339
no that's not it either
no, there is no default await, you'll get an error if you don't refer it yourself
$ lein repl :connect 61754
Connecting to nREPL at 127.0.0.1:61754
Could not resolve symbol: nrepl.core/version
user=> await
Could not resolve symbol: awaitsorry, I had overlooked that I require it from nbb.core all the time
in the code above, why doesn't the (.shutdown the-server) need an await? I would have assumed that this will always return a promise straight away?
Await still returns a promise but only adds a signal for the REPL to block