Here’s the translation of the Go code to PureScript, along with explanations in Markdown format suitable for Hugo:
Now, let’s define our hello handler function:
In this handler, we simulate some work by waiting for 10 seconds. We use the race function to compete between the delay and an immediate completion. If the delay wins, we send a successful response. If the immediate completion wins (simulating cancellation), we send an error response.
Now, let’s set up our main function to create and start the server:
To run this server, you would compile the PureScript code and run it with Node.js. Here’s how you might interact with it:
In the server logs, you would see:
Or if cancelled:
This example demonstrates how to handle long-running requests and potential cancellations in PureScript, similar to the concept of context in the original example. The Aff monad provides a way to work with asynchronous effects and handle cancellation, which is conceptually similar to Go’s context package.