# Help: Haskell async behavior I don't understand

**URL:** <https://discourse.haskell.org/t/help-haskell-async-behavior-i-dont-understand/12061>\
**Category:** Learn\
**Created:** [May 9, 2025, 9:59am UTC](https://discourse.haskell.org/t/help-haskell-async-behavior-i-dont-understand/12061 "2025-05-09T09:59:55Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![jaror](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/jaror/32/3271_2.png) [@jaror](https://discourse.haskell.org/u/jaror)\
**Post date:** [May 9, 2025, 11:27am UTC](https://discourse.haskell.org/t/help-haskell-async-behavior-i-dont-understand/12061/4 "2025-05-09T11:27:57Z")

</div>

You can do that like this:

```haskell
raceAnySuccess xs = do
  nref <- newIORef (length xs)
  let
    h :: SomeException -> IO a
    h e = do
      n <- atomicModifyIORef' nref (\n -> (n - 1, n - 1))
      if n == 0 then error "All threads failed" else forever
  runConcurrently $ asum $
    map
      (\x -> Concurrently $
        x `catches` [ Handler (\(e :: AsyncCancelled) -> throwIO e)
                    , Handler h ])
      xs

```

Instead of catching and rethrowing `AsyncCancelled` you can also just only catch the particular errors you’re interested in, if you know your domain.

By the way, you might see even better results if you use [`Conc`](https://hackage-content.haskell.org/package/unliftio-0.2.25.1/docs/UnliftIO-Async.html#t:Conc) from `unliftio`. Edit: I tried this and it also seems to be very slow.

---

_[View the full topic](https://discourse.haskell.org/t/help-haskell-async-behavior-i-dont-understand/12061)._
