lumo icon indicating copy to clipboard operation
lumo copied to clipboard

Support top-level await

Open pesterhazy opened this issue 8 years ago • 6 comments

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 avatar Aug 15 '17 23:08 pesterhazy

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

pesterhazy avatar Aug 15 '17 23:08 pesterhazy

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

bhurlow avatar Aug 22 '17 13:08 bhurlow

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

dupuchba avatar Sep 22 '17 07:09 dupuchba

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

pesterhazy avatar Sep 28 '17 07:09 pesterhazy

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

abhi18av avatar Mar 04 '18 17:03 abhi18av

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

bhurlow avatar Jul 17 '18 19:07 bhurlow