Search code examples
haskellasynchronouscontinuations

Continuation Monad and Asynchronous Responses


Assume that we have three synchronous functions that get data over HTTP (These could be API calls internally):

lookupUser :: String -> IO UserId
lookupUserCity :: UserId -> IO City
lookupLocation :: City -> IO Location

So, I could just do monadic composition lookupLocation <=< lookupUserCity <=< lookupUser to get the location of a user. But, since each of these calls will block, this will prevent the rest of the program from running.

Apparently, continuations help solve this, but I can't find any real examples of it. I think that the signatures would be rewritten a -> ContT r IO b, but I don't see how you could achieve a callback-like pattern with this. If anyone could show me (1) how to write transform :: (a -> IO b) -> (a -> ContT r IO b) or (2) could link to a real example of someone doing this, I would be grateful.


Solution

  • Continuations are just a pattern of working with functions, they have nothing to do with asynchronicity.

    What you need is forkIO or more high level abstractions presented by libraries like async.