Fork me on GitHub

I'd like to understand promesa.core/run! better. Just spent some time chasing a bug of mine that boils down to me thinking that ”promise aware” means it works a similar p/do!. Like so:

  (p/let [n 100
          !xs (atom [])]
    (p/run! (fn [x]
              (vscode/env.clipboard.writeText (str x))
              (p/let [cb-x (vscode/env.clipboard.readText)]
                (swap! !xs conj cb-x))
              (p/create (fn [resolve]
                          (js/setTimeout resolve 0))))
            (range n))
     (str "n: " n ", number of xs: " (count @!xs))))

  ;; Prints things like:
  ;;  n: 100, number of xs: 98
  ;;  n: 100, number of xs: 99
  ;;  n: 100, number of xs: 22
  ;;  n: 100, number of xs: 96
  ;;  n: 100, number of xs: 67
  ;;  ...

  (p/let [n 100
          !xs (atom [])]
    (p/run! (fn [x]
               (vscode/env.clipboard.writeText (str x))
               (p/let [cb-x (vscode/env.clipboard.readText)]
                 (swap! !xs conj cb-x))
               (p/create (fn [resolve]
                           (js/setTimeout resolve 0)))))
            (range n))
     (str "n: " n ", number of xs: " (count @!xs))))

  ;; Consistently prints:
  ;;  n: 100, number of xs: 100
  ;;  n: 100, number of xs: 100
  ;;  ...
The difference being that in the fixed version I wrap the three promises with p/do!. Can someone help me understand this better? 🙏


Forgot to say: The first version behaves well, without the setTimeout promise, afaict. This made me think I had understood p/run! for a while. Then I introduced the timeout wait...


run! Executes the callbacks as microtasks, meaning it delays the execution slightly to make the calls asynchronous. It also only runs each callback after the previous one finished and also the possibly returned promise finished. The difference between your version is that do! Makes the single promises inside the callback themselves also wait for the previous ones to finish while without do! They are all running concurrently and finish in their own time. Thus its also possible that run! By itself finishes before all readText are done as nothing is waiting for readText to finish. run! Only waits for the returned promise to finish (here the setTimeout promise)

🙏 1
❤️ 1

Thanks! That makes perfect sense.


Am I the only one wishing for p/doseq? 😀


That would probably also good to have, sure :lächeln: maybe it wouldnt be too hard to make a new macro for it in userspace but it would probably be a good addition in promesa.



(defmacro p-doseq [bindings & body]
  (let [syms (vec (take-nth 2 bindings))
        gen-syms (mapv gensym syms)]
    `(let [values# (for ~bindings ~syms)]
       (p/run! (fn [~gen-syms]
                 (-> (p/all ~gen-syms)
                     (p/then (fn [~syms] (p/do! [email protected])))))

(defn deliver-late [x]
  (p/create (fn [resolve] (js/setTimeout #(resolve x) 0))))

(p-doseq [x [(deliver-late -1) 0 1]
          y [1  2 3 (deliver-late 4)]]
         (prn x y))


Seems to be working


Or even simpler if the collection arguments cannot be promises as well:

(defmacro p-doseq [bindings & body]
  (let [syms (vec (take-nth 2 bindings))]
    `(let [values# (for ~bindings ~syms)]
       (p/run! (fn [~syms] (p/do! [email protected]))

(p-doseq [x [-1 0 1]
          y [1  2 3 4]]
         (delay 1000)
         (prn x y))


As I just fell into a small rabbit-hole I also made a p/for 😄


Fantastic! You should PR them to promesa, me thinks.


I was wishing for p/for last night as well. 😀


@U0ETXRFEW I'll see if I'll make the PR 😉

(defmacro p-for [bindings & body]
  (let [syms (vec (take-nth 2 bindings))]
    `(let [values# (for ~bindings ~syms)]
       (p/loop [result# (list)
                value# (first values#)
                rest# (rest values#)]
         (p/let [exec-result# (->
                               (p/promise value#)
                               (p/then (fn [~syms] (p/do [email protected]))))
                 next-result# (conj result# exec-result#)]
           (if (empty? rest#)
             (reverse next-result#)
              (first rest#)
              (rest rest#))))))))
Here the p/for if you want it in user-space 🙂

🙏 1

There is probably a better solution to the p/for though. I will probably rewrite it if I PR


(defmacro p-for [bindings & body]
  (let [syms (vec (take-nth 2 bindings))]
    `(let [values# (for ~bindings ~syms)]
        (reduce (fn [results# ~syms]
                  (p/let [prev-results# results#
                          result# (p/do! [email protected])]
                    (conj prev-results# result#)))
                (p/promise (list))
        (p/then reverse)))))
This may be better :thinking_face:


Looks simpler. Can you elaborate a bit on why one would be better than the other? Only if you have the time. And for the sake of me learning some macro author things. 😃


one thing to note is that this p-for isn't lazy


(there isn't a way to do it lazily with promises, since promises are eager)

🙏 1

I wouldnt say one is strictly better than the other. But the second one is more readable which is good enough for me :-) Yes, none of them are lazy. One thing that I noticed is that there are too many possible versions of those macros so im not sure in what forms they would best come. It would be possible to make them execute all concurrently, sequential (as now) and for both versions it would also be possible to await promises that would be coming in from the sequences as values.

oliver marks14:05:48

["ol" :as ol]
["ol/View" :refer [View]]
["ol/layer/Tile" :refer [TileLayer]]
["ol/source/XYZ" :refer [XYZ]]

(new XYZ {:url "https://{a-c}.})
I am translating this code to clojurescript I have the above import but XYZ seems to fail with ".node$module$ol$source$http://XYZ.XYZ is not a constructor" not even sure if this is a js issue but strange that the previous imports and there contructors worked but not the XYZ one am I missing something ? tend to avoid using node libraries as I always hit issues like this 😞


@olymk2 see the translation table in this section your requires are translated incorrectly if the package is actually ESM code. might also just be ["ol/source/XYZ" :as XYZ] in case it is not

oliver marks15:05:08

Thanks I will read through that and give it a try 🙂