anmonteiro / lumo

Fast, cross-platform, standalone ClojureScript environment
Eclipse Public License 1.0
1.89k stars 84 forks source link

Support top-level await #233

Open pesterhazy opened 6 years ago

pesterhazy commented 6 years ago

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.

pesterhazy commented 6 years ago

FWIW, this could be implemented as a repl special, along the lines of require.

bhurlow commented 6 years ago

@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?

dupuchba commented 6 years ago

what about extending ReadPort from core.async ? I am working with Promise too and it's not fun ^^

pesterhazy commented 6 years ago

@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.

abhi18av commented 6 years ago

@anmonteiro could you please update us whether this feature is coming soon in lumo scripts soon ?

bhurlow commented 5 years ago

has anyone attempted an "async" repl? would love to use one, even if just for top level await on promises