lumo
lumo copied to clipboard
Support top-level await
Most node APIs are asynchronous, often based on promises. Experimenting with promises from the REPL is not as nice as it could be however:
cljs.user=> (defn shortly [ms v] (js/Promise. (fn [resolve reject] (js/setTimeout #(resolve v) ms))))
#'cljs.user/shortly
cljs.user=> (-> (shortly 2000 :foo) (.then prn))
#object[Promise [object Promise]]
cljs.user=> :foo
It would be great to be able to request a promise synchronously from the REPL:
cljs.user=>(await (shortly 2000 :foo))
:foo
cljs.user=>
The REPL would block until the promise is resolved or until the user hits ^C.
For JS, the feature has landed in the Chrome devtools and Safari already, and it's being discussed in node.
FWIW, this could be implemented as a repl special, along the lines of require.
@pesterhazy I'm curious to hear what the clojurescript plan for support for await given that it's a keyword and not a generic function. However just for repl purposes, it seems like an await macro that expands to callbacks might suffice?
what about extending ReadPort from core.async ?
I am working with Promise too and it's not fun ^^
@bhurlow, a macro won't enable the same experience. Inevitably any fn call returns to the prompt before the async result arrives. Printing out the result will print over the prompt - overall janky.
The feature you'd need to prevent this is some way to instruct the repl to block until a promise is resolved.
In fact this is quite different from regular js await.
@anmonteiro could you please update us whether this feature is coming soon in lumo scripts soon ?
has anyone attempted an "async" repl? would love to use one, even if just for top level await on promises