clojurescript 2024-12-13

A quick search is failing me: does a ClojureScript seq satisfy JavaScript's iteratable protocol?

You can check in the REPL. ;) IIRC - no, but there's some function for that.

es6-iterator-seq will turn an ES6 iteration into a Clojure seq.

It looks like it just works as I'd hope, awesome. I need to fire off a bunch of things that'll return promises and wait for them all, wasn't sure if I'd have to jump through any hoops to do it.

(let [p1 (.resolve js/Promise 1)
      p2 1337
      p3 (.resolve js/Promise "foo")]
  (-> (.all js/Promise (seq [p1 p2 p3]))
      (.then (fn [values]
               (.log js/console values)))))

Is that seq necessary though?

it does, cljs.core/es6-iterable macro adds JS iterable protocol to all collection types

👍 1
😀 1

That seq in the sample was just for testing, the real code is something like:

(.all js/Promise (for [blah blahs] ...))

(defn iter->seq [iter] (go-loop [acc []] (let [el (<p! (.next iter))] (if (.-done el) acc (recur (conj acc (.-value el)))))))

(async/take! (iter->seq (.all js/Promise) #(doseq [v %] (.dir js/console v)))

A complete overkill to use core.async unless you're already using it. Especially when the built-in js/Promise.all works just fine.