andrewMacmurray/elm-concurrent-task/2.0.0

ConcurrentTask.Process

A drop in replacement for elm/core's Process.sleep.

The JavaScript runner has this task builtin by default. If needed it can be overridden like so:

NOTE: The custom example is the same as the built-in implementation.

import * as ConcurrentTask from "@andrewmacmurray/elm-concurrent-task"


ConcurrentTask.register({
  tasks: {},
  ports: app.ports,
  builtins: {
    sleep: customSleep
  }
});


function customSleep(ms: number): Promise<void> {
  return new Promise((resolve) => setTimeout(resolve, ms));
}
sleep : Int -> ConcurrentTask x ()

Wait for a number of milliseconds before continuing with the next Task.

A direct replacement for elm/core's Process.sleep.

withTimeout : Int -> a -> ConcurrentTask x a -> ConcurrentTask x a

Cancel a task, succeeding with the given value if the task takes longer than the given milliseconds.

import ConcurrentTask exposing (ConcurrentTask)
import ConcurrentTask.Http as Http
import ConcurrentTask.Process


type Fruits
    = Cached (List String)
    | Loaded (List String)


loadFruits : ConcurrentTask Http.Error Fruits
loadFruits =
    getFruits
        |> ConcurrentTask.withTimeout 2000 (Cached [ "Apple", "Orange", "Banana" ])

If getFruits takes longer than 2000ms the task will succeed with the Cached value.