How to do fire-and-forget HTTP requests in Elm?

My need for fire-and-forget requests is analytics.

With elm/http's Http.request I can only get requests that give me something back and I have to explicitly handle it in some update case (like NoOp).

Is there a way (perhaps with Task requests) that would allow me to ignore the response altogether?

2 Likes

I think your options are either (1) the NoOp approach you reference above or (2) use ports and send the request in JavaScript. Since ports use Cmd’s and not Tasks, you do not explicitly have to handle the error.

Http.request uses Cmds too (not Tasks), the difference to ports is that it emits the Msg with the response, while ports are really fire-and-forget.

We use ports and JS for analytics too but I think allowing for fire-and-forget requests in Elm would ake sense.

Http.request uses a Task under the hood, and Tasks by design do not allow you to forget the response.

They may make sense (I’m not sure) but they aren’t in the language now. You chose the Learn tag on discourse, so I’m trying to stick with your current options :slightly_smiling_face:

Could you use the task based http api (is it still there?) and then andThen the task with some task that infinite loops.

That task should then have the type signature Task Never Never and you can Task.perform Basics.never task it to get a command. Then you can pass that command to the runtime?

I am not saying you should do that, but I think you could.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.