This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-04-29
Channels
- # announcements (1)
- # babashka (15)
- # beginners (37)
- # calva (94)
- # cider (3)
- # clj-kondo (17)
- # cljsrn (2)
- # clojure (45)
- # clojure-europe (39)
- # clojure-germany (1)
- # clojure-norway (2)
- # clojurescript (16)
- # component (18)
- # conjure (1)
- # cursive (13)
- # datalevin (3)
- # datomic (12)
- # docker (2)
- # duct (5)
- # eastwood (2)
- # emacs (4)
- # events (8)
- # fulcro (8)
- # inf-clojure (5)
- # kaocha (8)
- # lsp (24)
- # malli (11)
- # meander (3)
- # off-topic (19)
- # polylith (11)
- # remote-jobs (4)
- # sci (61)
- # shadow-cljs (9)
- # spacemacs (34)
- # sql (10)
- # tools-deps (27)
- # xtdb (10)
In Clojure, I can use java.util.concurrent.CyclicBarrier
to achieve the purpose of only continuing when all the dependent tasks have finished. How can I achieve the same purpose in Clojurescript / JS land?
A typical example would be, when we start a system, first start all subsystems, and continue only when all subsystems have finished starting.
Given that JS is inherently single-threaded, there are two options:
• Run all tasks sequentially
• Run them asynchronously and then use Promise.all
.
My first plan was go with Run all tasks sequentially
, but seems there is no way for a non-async function to wait for the completion of an asynchronous function.
For Run them asynchronously and then use
Promise.all``
Not sure returning a promise from task fits in the cljs world.
CLJS world is full of interop, so I'd consider it fine. :)
Especially given that one task already involves an async fn - assuming we're talking about native JS async capabilities and not core.async
, you're already using promises, albeit potentially implicitly.
If you're already using core.async
elsewhere and if you really want to avoid promise interop, then you can use core.async
as well.
Sounds like the core.async
approach is a little bit verbose to implement. Each task could return a close-ch, and we have a goroutine that checks each close-ch has closed and then continue.
If 9 out of 10 tasks are synchronous, then just run them synchronously but in the same go
block. You don't need to make them all async.
JS is colored, so if you use anything async, everything all the way back to the entry point has to be async as well.