Whatβs the suggested way to deal with async interables from cljs? Iβm using core async so was looking something like <!p but found an open issue Maybe other lib?
We have had a lot of success with https://github.com/funcool/promesa their p/let implementation solves so much pain for us
Great, thanks! Didnβt know they support iterables
Oh wait me neither π
I ran into this a little while back and stole other peoples work (linked in source here)... https://github.com/cormacc/cljserial/blob/master/src/stack/apis/async_iterator.cljs And a usage example here.... https://github.com/cormacc/cljserial/blob/master/src/stack/apis/opfs.cljs#L50
I would absolutely just go the simplest route of interop.
(-> async-iter
(.next)
(.then (fn iterate [item]
(do-stuff-to item)
(-> async-iter
(.next)
(.then iterate)))))
Or something like that, haven't tested it.
I tend to avoid core.async in CLJS at all costs.Ah, have to check for (.-done item), and item is not actually the item. The actual item is apparently (.-value item).
@p-himik You and me both, I will only reach for core.async if there is really good reason to do that
But I also avoid Promesa and other other conveniences. :) I've been badly bitten before and the benefit of a thin wrapper isn't worth it.
Our backends are also nodejs and without some p/let macros our code would get very unwieldy very fast
But fair enough
Pestered ChatGPT enough to get me a full (but still untested) function:
(defn process-async-iterator [async-iterator callback]
(letfn [(handle-next [result]
(let [done (.-done result)
value (.-value result)]
(when-not done
(callback value)
(-> (.next async-iterator)
(.then handle-next)))))]
(-> (.next async-iterator)
(.then handle-next))))Hehe squint makes this so much better
If I have async iterator it works the same way as generator? Simple doseq and await on item?
in squint yes, but in normal CLJS no, since CLJS doesn't support await
promesa has p/doseq though (which I contributed π )
I guess you could also just do (js/Promise.all (async-generator-fn)) and then use .then to do whatever you want with the array of values