Fork me on GitHub
#cljsrn
<
2018-11-22
>
fabrao15:11:54

Hello all, anyone worked with Promise in re-natal?

fabrao15:11:50

vikeri how do you work with AsyncStorage and Promise stuffs?

vikeri15:11:43

@fabrao We have a helper function that puts the results of a promise on a core.async channel:

(defn promise->chan
  "Takes a promise and a tag and returns a channel instead
  Puts on the channel: [tag error/nil result/nil]"
  [promise tag]
  (let [ch (async/chan)]
    (-> promise
        (.then #(async/put! ch [tag % nil]))
        (.catch #(async/put! ch [tag nil %])))
    ch))
Then we have a normal core.async workflow using those channels.

wilkerlucio10:11:55

tip: if you use a async/promise-chan you can also get the promise semantics (always output same value once its ready)

vikeri15:11:15

But for quick stuff you can just thread it through .then and .catch like normal js-interop

fabrao15:11:16

I did it but no results, like this

(.then
       (.allDocs @local-database #js {:include_docs true :limit nil})
       #(js/console.log (.-rows %))
       )

fabrao15:11:10

or is that wrong what I´m doing?

vikeri15:11:52

I’d add a catch statement as well. But other than that it looks good. So there’s probably an error somewhere in your usage of the AsyncStorage API

vikeri15:11:09

Why do you reference AsyncStorage in an atom?

fabrao15:11:11

It´s just for testing, I´ll change after

vikeri15:11:44

Ok, don’t see the point of that though, even for testing.

fabrao15:11:16

(->
       (.allDocs @local-database #js {:include_docs true :limit nil})
       (.then #(js/console.log (.-rows %)))
       (.catch #(js/console.log %))) 
the log never came to log

vikeri15:11:26

And you’re sure you have data in the storage?

vikeri15:11:35

I’d try setting and getting data instead

vikeri16:11:02

And also use AsyncStorage directly without the atom

fabrao16:11:08

so, are saying that if no data, no callback?

vikeri16:11:18

No idea, but I’d start with setting the data at least so that I know there’s something there

fabrao16:11:07

sorry, I made a mistake, the database is PouchDB with AsyncStorage adapter

vikeri16:11:07

Ok, haven’t used that unfortunately

fabrao16:11:00

so, how do you use promise->chan ?

fabrao16:11:33

tag you use for identify data?

vikeri16:11:26

Yep, in theory you can add another argument that is an existing channel

vikeri16:11:34

Then you’d need to know which data you got back

fabrao16:11:52

I see, I´ll try to use it

vikeri16:11:39

@fabrao Also, @pesterhazy did a great talk where he explores the different concurrency models in js/cljs: https://www.youtube.com/watch?v=rV1gTJ2wsZg

fabrao16:11:49

@vikeri many thanks, I´ll see it

vikeri16:11:58

:thumbsup: