Our first example demonstrates the use of timers in PureScript. Timers allow us to execute code at some point in the future or repeatedly at some interval. We’ll focus on timers in this example.
To run this program, you would typically compile it with the PureScript compiler and then execute the resulting JavaScript. The exact commands might vary depending on your project setup, but it could look something like this:
The first timer will fire approximately 2 seconds after we start the program, but the second should be stopped before it has a chance to fire.
In this PureScript version, we use the Effect.Timer module to create and manage timers. The setTimeout function is used to create a timer, and clearTimeout is used to cancel it. We use launchAff_ and delay from the Effect.Aff module to handle asynchronous operations, which is similar to how channels are used in the original example for synchronization.
Note that PureScript, being a purely functional language, handles side effects and asynchronous operations differently from imperative languages. The use of Effect and Aff types helps manage these operations in a pure and composable way.