Fork me on GitHub
#core-async
<
2024-05-23
>
Nim Sadeh20:05:29

Is mult /`tap` the correct way to "keep" a value from a core.async operation so that it can be used by multiple go-blocks? The workflow I am thinking of looks something like this:

(let [fetch-result (do-work-that-returns-chan x y)
      action-1 (go (-> fetch-result
                       (<!)
                       (do-work-1)))
      action-2 (go (-> fetch-result
                       (<!)
                       (do-work-2))
])              
where the actions can be async. Of course, I could put all the action inside one go block, but then I'd have to put together things that I would like to be able to test separately, and it just doesn't seem ideal/composable.

jjttjj21:05:26

in this situation I'd lean towards something like this instead:

(go
  (let [result (<! (do-work-that-returns-chan x y))
        action-1     (go (-> result (do-work-1)))
        action-2     (go (-> result (do-work-2)))]
    ))

jjttjj21:05:25

alternatively you could use a promise-chan

(let [fetch-result-prom (a/pipe (do-work-that-returns-chan x y) (a/promise-chan))
      action-1     (go (-> fetch-result-prom
                           (<!)
                           (do-work-1)))
      action-2     (go (-> fetch-result-prom
                           (<!)
                           (do-work-2))
                       )]
  )

jjttjj22:05:35

> Of course, I could put all the action inside one go block, but then I'd have to put together things that I would like to be able to test separately, and it just doesn't seem ideal/composable. Imo the ideal is to have pure functions, test those, then wrap them all where they need to "come together"