# Process.sleep: how can it fail and how can it be canceled?

**URL:** <https://discourse.elm-lang.org/t/process-sleep-how-can-it-fail-and-how-can-it-be-canceled/10166>\
**Category:** Learn\
**Created:** [March 6, 2025, 6:29pm UTC](https://discourse.elm-lang.org/t/process-sleep-how-can-it-fail-and-how-can-it-be-canceled/10166 "2025-03-06T18:29:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![akiross](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/akiross/32/4723_2.png) [@akiross](https://discourse.elm-lang.org/u/akiross)\
**Post date:** [March 6, 2025, 6:29pm UTC](https://discourse.elm-lang.org/t/process-sleep-how-can-it-fail-and-how-can-it-be-canceled/10166/1 "2025-03-06T18:29:25Z")

</div>

Hello,

I think it’s my first time using `Process.sleep`, and I don’t even need to cancel it, but I’m curious about the current API.

Sleep is defined like this:

[sleep](https://package.elm-lang.org/packages/elm/core/latest/Process#sleep) : [Float](https://package.elm-lang.org/packages/elm/core/latest/Basics#Float) → Task x ()

It is my understanding that, being a `Task`, it can fail, but in which ways can it fail?

Also, I noticed there’s not an explicit way to cancel a timeout, so I went for the elm source code and found [this](https://github.com/elm/core/blob/65cea00afa0de03d7dda0487d964a305fc3d58e3/src/Elm/Kernel/Process.js#L9): there’s a `clearTimeout` in there, so I wonder what’s going on: is there a way to use it?

Again, not that I need this, but what’s the story around this?  
Thank you!

---

<div class="post-metadata">

**Author:** ![G4BB3R](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/g4bb3r/32/4544_2.png) [@G4BB3R](https://discourse.elm-lang.org/u/G4BB3R)\
**Post date:** [March 6, 2025, 6:51pm UTC](https://discourse.elm-lang.org/t/process-sleep-how-can-it-fail-and-how-can-it-be-canceled/10166/2 "2025-03-06T18:51:14Z")

</div>

I don’t know the motivation for it to be a failable Task, and it’s clearly an unfinished API.  
But I think Process.kill is the answer.

> **[Process - core 1.0.5](https://package.elm-lang.org/packages/elm/core/latest/Process#kill)**

---

<div class="post-metadata">

**Author:** ![MartinS](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/martins/32/3137_2.png) [@MartinS](https://discourse.elm-lang.org/u/MartinS)\
**Post date:** [March 6, 2025, 6:52pm UTC](https://discourse.elm-lang.org/t/process-sleep-how-can-it-fail-and-how-can-it-be-canceled/10166/3 "2025-03-06T18:52:03Z")

</div>

Tasks in general can fail, but this Task in particular cannot. You can tell by looking at the type signature: `sleep : Float → Task x ()`. The error type is `x` and that type variable isn’t present anywhere else in the signature. That means there’s no way for sleep to create an error of type `x`.

If it isn’t clear why that’s the case, try this example:

```elm
myError : Result x ()
myError = 
    Err ? -- What should I replace the ? with

```

the answer is you can’t. The only thing that would work here is `Ok ()`\*

As for `clearTimeout`. I’m not sure exactly what that is there for. Maybe it’s used internally or maybe it’s the start of some API for cancelling Process.sleep that hasn’t been finished yet (similar to [Process.spawn](https://package.elm-lang.org/packages/elm/core/latest/Process#spawn) and [Process.kill](https://package.elm-lang.org/packages/elm/core/latest/Process#kill))

If you did need a way to cancel a Process.sleep, I’d do it by updating a field in your Model that the Task checks when it triggers an update to not do anything.

\*Sure, you could use `Debug.todo ""` or use a function that never halts but that just delays the issue until runtime.

---

<div class="post-metadata">

**Author:** ![Bram](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/bram/32/4862_2.png) [@Bram](https://discourse.elm-lang.org/u/Bram)\
**Post date:** [March 6, 2025, 9:40pm UTC](https://discourse.elm-lang.org/t/process-sleep-how-can-it-fail-and-how-can-it-be-canceled/10166/4 "2025-03-06T21:40:16Z")

</div>

A secondary feature of a `Task` is that you can stack them using [Task.andThen](https://package.elm-lang.org/packages/elm/core/latest/Task#andThen). This allows you to execute another `Task` after waiting for a certain number of seconds.

## Practical example

As the developer of the [Matrix SDK](https://package.elm-lang.org/packages/noordstar/elm-matrix-sdk-beta/latest/), this is incredibly useful for **ratelimits**. I have a certain `Task` type that does an HTTP request to the server, but sometimes the server returns the following JSON:

```json
{
  "errcode": "M_LIMIT_EXCEEDED",
  "error": "Too many requests",
  "retry_after_ms": 2000
}

```

Using `Process.sleep`, I can redefine my `Task` with the following function:

```elm
{-| Catch ratelimits
-}
allowRatelimits : Task a b -> Task a b
allowRatelimits task =
    task
        |> Task.mapErr
            (\data ->
                if data.errcode == "M_LIMIT_EXCEEDED" then
                    Process.sleep data.retryAfterMs
                        |> Task.andThen (allowRatelimits task)
                else
                    Task.fail data
            )

```

This is a complex function, but it basically does the following:

1. It makes the HTTP call.
2. If it gets the ratelimit error, it waits for that time using `Process.sleep`.
3. It tries again.

## TLDR

`Process.sleep` lets you execute another `Task` after a minimum number of milliseconds. It helps you execute tasks in the future.

---

<div class="post-metadata">

**Author:** ![akiross](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/akiross/32/4723_2.png) [@akiross](https://discourse.elm-lang.org/u/akiross)\
**Post date:** [March 7, 2025, 9:14am UTC](https://discourse.elm-lang.org/t/process-sleep-how-can-it-fail-and-how-can-it-be-canceled/10166/5 "2025-03-07T09:14:41Z")

</div>

Thank you all for the great answers! Now it’s clearer, but I still have to take a look at `Process.kill` and understand how it combines with `Process.spawn`.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [March 17, 2025, 9:15am UTC](https://discourse.elm-lang.org/t/process-sleep-how-can-it-fail-and-how-can-it-be-canceled/10166/6 "2025-03-17T09:15:21Z")

</div>

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