From 458784d70f664f3af9b98655505ca93e72610376 Mon Sep 17 00:00:00 2001
From: Jens Nolte <git@queezle.net>
Date: Thu, 22 Jul 2021 00:25:38 +0200
Subject: [PATCH] Add Async utilities

---
 src/Quasar/Core.hs | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/Quasar/Core.hs b/src/Quasar/Core.hs
index f4764d1..a92b890 100644
--- a/src/Quasar/Core.hs
+++ b/src/Quasar/Core.hs
@@ -11,6 +11,10 @@ module Quasar.Core (
   async,
   await,
   runAsyncIO,
+  awaitResult,
+
+  -- * Async helpers
+  mapAsync,
 
   -- * AsyncVar
   AsyncVar,
@@ -72,7 +76,6 @@ class IsAsync r a | a -> r where
   toAsync = SomeAsync
 
 
-
 data Async r = forall a. IsAsync r a => SomeAsync a
 
 instance IsAsync r (Async r) where
@@ -141,6 +144,16 @@ await = AsyncIO . pure . toAsync
 runAsyncIO :: AsyncIO r -> IO r
 runAsyncIO = async >=> wait
 
+awaitResult :: AsyncIO (Async r) -> AsyncIO r
+awaitResult = (await =<<)
+
+
+mapAsync :: (a -> b) -> Async a -> AsyncIO (Async b)
+-- FIXME: don't actually attach a function if the resulting async is not used
+-- maybe use `Weak`? When `Async b` is GC'ed, the handler is detached from `Async a`
+mapAsync fn = async . fmap fn . await
+
+
 
 -- ** Forking asyncs
 
-- 
GitLab