Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequential computation macro #148

Open
chr15m opened this issue Dec 17, 2023 · 0 comments
Open

Sequential computation macro #148

chr15m opened this issue Dec 17, 2023 · 0 comments

Comments

@chr15m
Copy link

chr15m commented Dec 17, 2023

It would be great if there was a way to run a series of promises sequentially, waiting for the previous one to finish, and return a vec of all promises for use with p/all. This is different to creating a map of promise returning functions as that would run in parallel not sequentially. See discussion on Slack here.

Here is an implementation using reduce:

(p/let [result
          (p/all
            (reduce
              (fn [col panel-choice]
                (conj col
                      (p/let [_ (last col)]
                        (something-returning-promise panel-choice))))
              []
              panel-choices))]
    (print "reduce" result))

Here is an implementation using p/loop and p/recur:

(p/let [result
          (p/loop [choices panel-choices results []]
            (if (empty? choices)
              results
              (p/let [result (something-returning-promise
                               (first choices))]
                (p/recur (rest choices)
                         (conj results result)))))]
    (print "loop/recur" result))

You can test this in nbb with promises.cljs here:
https://gist.github.com/chr15m/c5f943b6aa2c6089c04f04897b3cef81

@chr15m chr15m changed the title Sequential computation option Sequential computation macro Dec 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant