Let's say, hypothetically, I have to ping a REST API endpoint, and then wait for a message over a websocket. What's the preferred method for wrapping these up into one synchronous function? For example, do-thing
should ping the endpoint and not return until the websocket message is received. I could wait on a semaphore or use a deferred promise, I think. Any recommendations?
I don't think you can do that - WebSocket API is async, you cannot make it sync.
I'm not trying to make it synchronous. I'm trying to wrap a couple of asynchronous operations into one function that doesn't return until everything has completed.
Unfortunately <!!
and >!!
aren't available in clojurescript
If we restrict ourselves to the JS world, then that function:
- has to be marked as async
- has to await
on the relevant promise
It will not "return" if it's used with another await
(quotes because await
and async
are just a sugar anyway). But it will return a promise just fine if used without await
.
With that being said, CLJS has no support for async
and await
keywords.
You cannot block in JS. There's just no way.
If your code absolutely must rely on some blocking function then that code has to be restructured to support the scenario with WebSocket.
Locking won't block? https://cljs.github.io/api/cljs.core/locking
As you can see from its source code, it does absolutely nothing but ignores x
.
I don't know why. My bet is that it's there simply to make it easier to write code that works with both CLJ and CJLS.
:man-facepalming:
Imagine you have a language where you have just a single thread and an event queue. Nothing more. How would you block an execution there to wait for some event in the queue? The answer is, you cannot do that. And that's basically what JS is - a single-threaded VM with a queue.
I understand. Thanks
Hi, I have a weird behavior with javascript interop that I don’t understand I’m working with https://www.highcharts.com/ and https://github.com/highcharts/highcharts-react. I’m trying to get the property processedYData from inside one of the chart objects called Series. If I print in the console the series object the property appears
(.log js/console "series=" series)
This is part of what is shown in the chrome console:
series=
x {hcEvents: {…}, axisTypes: Array(2), coll: "series", colorCounter: 0, remove: ƒ, …}
addPoint: λ[]
afterAnimate: λ[]
alignDataLabe
……
processedYData: (1052) [0, 0.95, 0.22, -1.36, 0.14, 0.53, 0.64, 0.97, 0.62, -0.91, -2.18, -1.67, -1.68, -0.77,
……
but when I log only the processedYData property it is empty
(.log js/console "processedYData=" (. series -processedYData))
chrome log
processedYData= []
What I’m doing wrong? What I’m missing? thanks in advanceThat's strange. Have you tried debugging it in Chrome? You can add (js-debugger)
in your code - it will trigger a breakpoint if that code is executed while the DevTools panel is open.
I didn’t know about that fn (js-debugger)
I will try, thanks
some times it appears some time it doesn’t or it’s outdated. I think I’m going to change my point of view
thanks @p-himik, you’re always helpful
is there an equivalent of an uberjar that targets node? In the optimal case I hand a virgin node a single file and it runs my cljs script/daemon for me on the target device
shadow-cljs with :target :node-script works for me in release mode