Bounded Nim threadpool worker pool with results

Nim
import std/threadpool  # build with --threads:on

proc work(n: int): int =
  if n == 4: raise newException(ValueError, "bad input: 4")
  n * n

var futures: seq[FlowVar[int]]
for i in 1 .. 8:
  let f = spawn work(i)
  futures.add f

var results: seq[int]
for f in futures:
  try: results.add ^f
  except ValueError as e: echo "task failed: ", e.msg
echo results