<div dir="ltr">I just wrote this helper:<div><br></div><div><div><font face="monospace">> import Control.Concurrent</font></div><div><font face="monospace">> import Control.Concurrent.STM</font></div><div><font face="monospace">> import Control.Exception.Safe</font></div><div><font face="monospace">> </font></div><div><font face="monospace">> pollT :: Int -> IO a -> IO (STM (Maybe a), Async b)<br></font></div><div><font face="monospace">> pollT delay act = do</font></div><div><font face="monospace">>   tv <- atomically (newTVar Nothing)</font></div><div><font face="monospace">>   as <-</font></div><div><font face="monospace">>     async . forever $ do</font></div><div><font face="monospace">>       r <- tryAny act</font></div><div><font face="monospace">>       case r of</font></div><div><font face="monospace">>         Left _ -> pure ()</font></div><div><font face="monospace">>         Right r' -> atomically (writeTVar tv (Just r'))</font></div><div><font face="monospace">>       threadDelay delay</font></div><div><font face="monospace">>   pure (readTVar tv, as)</font></div></div><div><br></div><div>I was sort of surprised not to find something like this in an existing library.</div><div><ol><li>Did I miss an existing implementation?</li><li>Any problems with this one?</li><li>Any suggestions for a better name?</li><li>Any thoughts on the general idea? That is, "run an action periodically, updating a TVar with the result".</li></ol><div>There's a couple of obvious variations that can be built on top of this, like retrying if the TVar is Nothing when reading from it, or writing Nothing to the TVar when the action fails rather than keeping the old value. Maybe passing in the old value to the action?</div></div></div>