Asynchrony

Consider an example of a web server tasked with handling incoming HTTP requests in a setting of a web application that manages an online forum. Consider that the requests in question are to be responded to with the data about all of the forum posts present in the database. This means that in order to reply to any given HTTP request, we first need to obtain the required data from the database. The solution may look as follows:

def handle(request: Request): Response = {  val posts: List[Post] = allPosts()  respond(posts)}

In the preceding example, the types and methods are declared as follows:

type Requesttype Responsetype Postdef allPosts(): List[Post]def respond[A](a: A): Response

However, this is not the only task that our application ...

Get Mastering Functional Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.