Search⌘ K

Synchronous Behavior and IO

Explore how to manage synchronous behavior and side effects in TypeScript by using the IO monad from fp-ts. Understand the distinction between pure and impure functions, how to wrap impure behavior like logging and time retrieval, and organize configuration values effectively.

Overview

We’ll ignore the function name for now and focus on how to create the Between. It should be clear that there are problems with purity when it comes to getting the start and end times because we want to show a certain range in regard to the current time. Consider the last three hours of metrics, for example. This means that the function for getting our Between returns different values depending on when it was invoked, which is impure behavior. How will we wrap it? In a Task? We could do that, but this is synchronous behavior, for which the IO monad is ideally suited. Similarly, IO is also what we normally use to wrap our logging, which is, after all, another synchronous side effect.

As noted, in a hybrid language like TypeScript ...