newtype Task x a
Task x a represents an asynchronous computation that can either succeed with a value of type
a, or fail with a value of type
If you're coming from Elm and it seems like a lot of the functions are missing, that's because their equivalents are hiding in the type class instances. They have been annotated below.
Functor (Task x)
Apply (Task x)
Applicative (Task x)
Task.sequence-> For this, we use the
sequencefunction that is a member of the Traversable type class. Note: we are not using a
Task, we're using the
Traversableinstance of whatever we want to sequence over. So while Elm's
Task.sequenceonly works over
Lists, we can sequence over anything that has a
Bind (Task x)
bind(the only difference is the order of the arguments)
Monad (Task x)
Alt (Task x)
(Semigroup a) => Semigroup (Task x a)
(Monoid a) => Monoid (Task x a)
lmap, which isn't a member of
Bifunctordirectly, but uses the
MonadEffect (Task x)
MonadThrow x (Task x)
MonadError x (Task x)
Parallel (ParTask x) (Task x)
makeTask, you need to create a function that takes a callback for both success and failure, and returns an effect that will cancel a task in the event of an error during parallel execution. If you do not need or do not care about cancelling a task, you can use
pure $ pure unit.
Here is how you would make a simple waiting task using js-timers:
wait :: ∀ x. Int -> Task x Unit wait ms = makeTask \aC _ -> do id <- setTimeout ms $ aC unit pure $ clearTimeout id
fromForeign :: forall x a. (ForeignCallback a -> ForeignCallback x -> Effect Canceler) -> Task x a
newtype ParTask x a
ParTask is the applicative that lets you run tasks in parallel via the parallel library.
You may not need to work with any
ParTask values directly. One of the most useful functions parSequence, which can take an array of
Task x a and executes them all in parallel, returning a
Task x (Array a).
If tasks are executing in parallel via
Apply instance (as is the case with
parSequence), when one of them fails, the cancelers are called for all the task that are currently running.