Fork me on GitHub
#re-frame
<
2022-11-15
>
mauricio.szabo14:11:17

Folks, any tips on handling things with promises? I have quite a lot of situations where my answers are inside promises and I'm 100% sure they won't break (they are only inside of promises because of the tool I have to use, not because they're truly async) but creating a reg-event-fx, with reg-fx and them reg-event-db just for this situations is becoming tedious...

p-himik18:11:15

> creating a reg-event-fx, with reg-fx and them reg-event-db just for this situations is becoming tedious... You can have a single reg-fx and a single reg-event-db for all promises - you'd just pass them the right data and that's it. You'd still need ad-hoc reg-event-fx handlers to pass just the right data, but that's the case with any soluiton.

mauricio.szabo19:11:34

I feel I can have some concurrency problems with this approach :thinking_face:. I honestly don't really know how would this work - a reg-event-fx that triggers this reg-fx, with... the promise result? The function that I want to call? I'm not sure how to wire this...

p-himik19:11:05

Probably something like:

(rf/reg-fx ::make-a-promise-you-can-keep
  (fn [{:keys [f then catch]}]
    (-> (fn-that-returns-a-promise)
        (.then (fn [result]
                 (rf/dispatch (conj then result))))
        (.catch (fn [error]
                  (rf/dispatch (conj catch error)))))))
This is without that extra reg-event-db so it still requires a custom event for each then/`catch`. But if all your promises do is store some data in app-db, then you can also make a generic event or two and then then/`catch` can become paths in app-db.