I'm working with clojurescript and localforage a promise based storage library. I have a structure set up like the following in localstorage as key value pairs
"names" : ["name1","name2","name3"]
Where then each "names" is a key for another set of values.
"name1": [val1,val2,val3]
I'm currently at the point where I have the list of names and now need to iterate through that list, get the values for each one, and then return a map with a format like:
{:name1 [val1,val2,val3] :name2 [val1,val2]}
To accomplish this, I came up with the following snippet:
(defn get-project-dates [project-map]
"Handles getting all the times/dates for project"
(loop [i 0
project-dates {}]
(if (= i (count project-map))
project-dates
(.then (.getItem localforage (nth project-map i)) (fn [promiseVal]
(recur (inc i) (conj project-dates {(key (nth project-map i)) promiseVale})))))))
Unfortunately this doesn't work as instead of recur going to the loop, it will go back to the (fn). This (fn) callback is however required as the (.getItem) call returns a promise that I can't access otherwise.
My question is then is there a way to get that promise value out and recur to the loop, or a better way overall to do this?
(.then (Promise/all (map #(.then (.getItem localforage %) (fn [v] [% v])) project-map)) (partial into {}))- leetwinski