Question

A REST call uses spray.io. It validates the request in that call using various functions including one in the middle that queries a data store. If all is ok then it writes to said data store. In this situation there are up to 4 IO actions: HTTP request, data read, data write and HTTP response. If I had decided to use the IO monad, how would I structure perform IO given I have some that may or may not be required (i.e. read and write stuff may not need to be done if some validation fails) interspersed with non IO functions? Presumably, first of all I could first ignore the spray stuff and think of the evaluation as starting after spray has done it's thing. But how do I compose the IO functions with non-IO functions? Do I have to lift the non-io stuff into an io monad?

Was it helpful?

Solution

The short answer is Futures. In order to be non-blocking your IO operations should return Futures that you map to other Futures. In Spray you can complete the request with a Future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top