Home > OS >  How to execute two effects and get their respective return values (which maybe a success or an error
How to execute two effects and get their respective return values (which maybe a success or an error

Time:10-09

I try to parallelize this sequential and fail fast solution:

 for
      cellTowersPayload <- CellHintTransformer.createGoogleApiPayload(cellAndWifiHints.cellHints).mapError(mapRequirementsFailure)
      googleMapsResultCellTowers <- googleMapsApi.call(cellTowersPayload)
      wifiHintsPayload <- WlanHintTransformer.createGoogleApiPayload(cellAndWifiHints.wifiHints).mapError(mapRequirementsFailure)
      googleMapsResultWifiHints <- googleMapsApi.call(wifiHintsPayload) // TODO paralleize with call cell towers
      mappedResult <- mapResult(googleMapsResultCellTowers, googleMapsResultWifiHints)
    yield mappedResult

The resulting solution should make the 2 calls in parallel and return both results (which may be success or failure each). So no fail fast if one of the calls fails

How do I do this idiomatic in ZIO?

CodePudding user response:

So, there are two things here. You can do operations in parallel by using .fork on the ZIO you want to parallelize and then do the join on those forked fibers.

The problem is that You need to somehow make sure both of them are executed even if the other fails, ZIO by default will cancel another fiber if one of fibers fails to minimize computations. We can use something like .either to make sure that ZIO neve fails but instead returns Either[E, A].

So, we would do something like:

for {
  zio1 <- firstOperation().fork().either
  zio2 <- secondOperation().fork().either
  result <- zio1.zip(zio2)
  tupleResult <- result.join
} yield tupleResult

CodePudding user response:

Answered here https://discord.com/channels/629491597070827530/630498701860929559/1027300896599724102 by Adam Fraser

 left.exit.zipPar(right.exit)
  • Related