2024-03-05 07:06:27 +00:00
|
|
|
import std/sequtils, chronos
|
2024-03-01 17:06:26 +00:00
|
|
|
|
2024-03-05 07:06:27 +00:00
|
|
|
proc allFuturesThrowing*(args: varargs[FutureBase]): Future[void] =
|
2024-03-01 17:06:26 +00:00
|
|
|
# This proc is only meant for use in tests / not suitable for general use.
|
|
|
|
# - Swallowing errors arbitrarily instead of aggregating them is bad design
|
|
|
|
# - It raises `CatchableError` instead of the union of the `futs` errors,
|
|
|
|
# inflating the caller's `raises` list unnecessarily. `macro` could fix it
|
2024-03-05 07:06:27 +00:00
|
|
|
let futs = @args
|
|
|
|
(
|
|
|
|
proc() {.async: (raises: [CatchableError]).} =
|
|
|
|
await allFutures(futs)
|
|
|
|
var firstErr: ref CatchableError
|
2024-03-01 17:06:26 +00:00
|
|
|
for fut in futs:
|
|
|
|
if fut.failed:
|
2024-03-05 07:06:27 +00:00
|
|
|
let err = fut.error()
|
|
|
|
if err of CancelledError:
|
2024-03-01 17:06:26 +00:00
|
|
|
raise err
|
2024-03-05 07:06:27 +00:00
|
|
|
if firstErr == nil:
|
|
|
|
firstErr = err
|
|
|
|
if firstErr != nil:
|
|
|
|
raise firstErr
|
|
|
|
)()
|
2024-03-01 17:06:26 +00:00
|
|
|
|
2024-03-05 07:06:27 +00:00
|
|
|
proc allFuturesThrowing*[T](futs: varargs[Future[T]]): Future[void] =
|
|
|
|
allFuturesThrowing(futs.mapIt(FutureBase(it)))
|
|
|
|
|
2024-03-28 08:42:31 +00:00
|
|
|
proc allFuturesThrowing*[T, E]( # https://github.com/nim-lang/Nim/issues/23432
|
2024-03-05 07:06:27 +00:00
|
|
|
futs: varargs[InternalRaisesFuture[T, E]]
|
|
|
|
): Future[void] =
|
|
|
|
allFuturesThrowing(futs.mapIt(FutureBase(it)))
|