Race conditions in value initialization w/clojurescript (reagent)

I’ve worked into a dumb rut. A combination of project-scope and deadlines means I’m working in raw reagent here. Given that I have to return an atom, what code-writing pattern will ensure I have my ajax values in that atom?

(def gonna-be-empty
  (let [from-ajax (get-outside-valmap)
        vals {:uid (:username from-ajax)}]
    (atom vals)))
;; {:uid nil} even if a moment later get-outside-valmap returned

When you say ajax I’m assuming that you are sending an actual request which means the result will only be available later when the async event completes. That means that the atom will initially be empty. Nothing can change that since it is impossible to “block” in JS.

The only option you can do is returning the atom immediately and swap the data in “later” in a callback passed to the ajax request.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.