Fork me on GitHub
#cljsrn
<
2017-01-21
>
ejelome12:01:24

oh, thanks @edwthomas, I was about to ask about the proper way 😄

ejelome21:01:48

... this is funny, now I have to deal with Promises, oh my

ejelome21:01:43

I probably should have used jquery for this, seems this is too much for cljs on its own

ejelome21:01:43

@edwthomas thanks for the example, but it felt short because I don't use re-frame as of the moment, only reagent, probably re-frame handles promises automatically, I think this http is eating to much of my time, not a friendly stuff to work with re-natal

edwthomas22:01:05

@ejelome the re-frame dispatch function is just a way to update the app state that’s stored in a reagent atom. It doesn’t have any knowledge of Javascript Promises that are used by the Fetch API.

edwthomas22:01:07

The json() method of the Response returns a Promise that you can handle using it’s then() method. That’s basically what I’m doing with the (.json response) function followed by the (.then (fn[]….) call.

ejelome23:01:32

edwthomas:

(.log js/console
        (-> (js/fetch "")
            (.then (fn [response]
                     (.json response)))
            (.then (fn [post]
                     (js->clj post :keywordize-keys true)))
            ))
I'm stuck with the Promise object: > Promise {_45: 0, _81: 0, _65: null, _54: null}

ejelome23:01:50

I've also tried your nested .then but same thing happens

ejelome23:01:48

you can try your example and will see it will keep on returning a Promise object:

(.log js/console
      (->
       (js/fetch "" (clj->js {:method "GET"}))
       (.then (fn [response]
                (-> (.json response)
                    (.then (fn [post]
                             (js->clj post :keywordize-keys true))))))))

edwthomas23:01:18

hmm sorry I’m heading out now but will try this sometime tomorrow. I basically copied this from a working app so it should have worked. I’ll try to check it out tomorrow.

ejelome23:01:56

no worries, you have given too many hints, I'll try to understand it better this time, thanks a bunch 🙂

edwthomas00:01:43

Great you finally got it work! Now even I can sign off because I was wondering why it didn’t work for you! haha

ejelome00:01:48

haha, right, with great mentors, things get easy 😄