hi there! I’ve been playing with with SCI recently in my small ClojureScript toy project (a kind of a computational notebook) and it was exactly what I needed and super easy to integrate. Really impressive work! 😍 It made me wonder: would the current design of SCI allow for passing bindings that execute asynchronously, but appear synchronous (blocking) to the caller? E.g., a program like (do (sleep 2000) (js/alert "Here"))
. This is, of course, trivial in Clojure where one can dereference a future (or use the <!!
macro in core.async
), but it could be possible in ClojureScript if SCI could interrupt on certain bindings, returning control to the caller and providing a continuation. Does this make sense? I’d love a synchronous scripting language that runs in a browser. (I’m more familiar with Clojure than ClojureScript so maybe there’s already a way to achieve this somehow via creative use of core.async
or native promises). Any pointers would be welcome 🙏
@chistyakov.artem I think @mauricio.szabo has done something similar in his #chlorine-clover project where he implemented a kind of let
macro that accepts promises as bindings. Maybe he could tell you more.
@chistyakov.artem It's this p/let
one:
https://github.com/mauricioszabo/atom-chlorine/blob/master/docs/extending.md
I found the integration I think, it uses the promesa library: https://github.com/mauricioszabo/repl-tooling/blob/2e8ffc9e989bf8a87b1458d251fab7287d25f8ce/src/repl_tooling/editor_integration/interpreter.cljs#L27
@chistyakov.artem I extracted a working example here: https://gist.github.com/borkdude/e926d02b0d44dde04d74364201172e9b It doesn't even use the promesa library, it's just pure promises.
oh wow, thank you so much @borkdude! I’ll look into this more tonight. The gist you shared is much better than what I have right now. I guess, your answer also implies that currently there’s no way to “abstract the async away” from the user like in my original sleep/alert
example, is that correct? I realize it might be out of scope for SCI, but it’s one of those things that seem possible in abstract 🙂
Sci doesn't have anything for this at the moment
Yeah, what I did was to simulate the p/let
from promesa into SCI. I was thinking if it should be possible to eval form by form and, if one return promises, await for it to resolve. This would remove the need for the let
binding for SCI too 🙂
reminds me of a thing I’ve promised you @borkdude…
@mkvlr No worries, it's not a trivial problem to achieve a solution for ;)