FsToolkit.ErrorHandling icon indicating copy to clipboard operation
FsToolkit.ErrorHandling copied to clipboard

Question: parallel async CE

Open ursenzler opened this issue 7 months ago • 5 comments

First, many thanks for the update.

I wondered what your thoughts are on a parallelAsyncCE? There exist parallelResultCE and parallelValidationCE, but no parallelAsyncCE. Is it because there is no asyncCE because that is included in the F# Core libraries, or are there other reasons?

Happy coding, Urs

ursenzler avatar Jun 13 '25 08:06 ursenzler

I think this would be a good addition since and! for the official async CE was rejected.

There might be overlap with IcedTasks, however: https://github.com/TheAngryByrd/IcedTasks/blob/7c9ab05fc64e4f2dca99d130eae91a0243516cf8/src/IcedTasks/ParallelAsync.fs#L130-L133

Note that there are several different ways to "zip" two asyncs. This library uses Async.Parallel, which is different to IcedTasks.

njlr avatar Jun 13 '25 09:06 njlr

The different implementation possibilities are the reason why I'm not confident in implementing this correctly on my own. I don't know the implications of the different approaches.

ursenzler avatar Jun 13 '25 09:06 ursenzler

You can use the Async.parallelMap2 implementation from this library with regular async:

#r "nuget: FsToolkit.ErrorHandling, 5"

open FsToolkit.ErrorHandling

let foo =
  async {
    return 1
  }

let bar =
  async {
    return 2
  }

async {
  let! x, y = Async.parallelMap2 (fun x y -> x, y) foo bar

  return x + y
}

Or with CE syntax:

#r "nuget: FsToolkit.ErrorHandling, 5"

open FsToolkit.ErrorHandling

type AsyncBuilder with
  member this.MergeSources(x, y) =
    Async.parallelMap2 (fun i j -> i, j) x y

let foo =
  async {
    return 1
  }

let bar =
  async {
    return 2
  }

async {
  let! x = foo
  and! y = bar

  return x + y
}

njlr avatar Jun 13 '25 09:06 njlr

Yeah, IcedTasks has an parallelAsync implementation, however it doesn't support Fable like FsToolkit does. I'll have to think about if it makes sense to put it here because of that.

njlr's solution would work if you need it for Fable or you can used IcedTask's implementation.

TheAngryByrd avatar Jun 16 '25 16:06 TheAngryByrd

We use the implementation of IcedTask in our own extension to the normal AsyncBuilder. I guess it would be nice to have a parallelAsync in FsToolkit, though.

ursenzler avatar Jun 18 '25 08:06 ursenzler