Skip to content

add finallyDo to helper function #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions src/ConcurrentTask.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module ConcurrentTask exposing
, onResponseDecoderFailure, onJsException
, mapError, onError
, succeed, fail, andThen
, fromResult, andThenDo, return, debug
, fromResult, andThenDo, return, debug, finallyDo
, batch, sequence
, map, andMap, map2, map3, map4, map5
, attempt, Response(..), UnexpectedError(..), onProgress, Pool, pool
Expand Down Expand Up @@ -68,7 +68,7 @@ Lift `UnexpectedError`s into regular task flow.

These are some general helpers that can make chaining, combining and debugging tasks more convenient.

@docs fromResult, andThenDo, return, debug
@docs fromResult, andThenDo, return, debug, finallyDo


# Batch Helpers
Expand Down Expand Up @@ -708,6 +708,40 @@ debugLog tag message =
}


{-| Always executes the Task, regardless of whether the previous Task has succeeded or failed.

The behavior is similar to JavaScript's `finally` block in a `try-catch-finally` statement.

This can, for example, be used to ensure that a resource is always released after being locked:

import ConcurrentTask exposing (ConcurrentTask)

lockResource : ConcurrentTask String ()
lockResource =
succeed ()

unlockResource : ConcurrentTask String ()
unlockResource =
succeed ()

performOperation : ConcurrentTask String ()
performOperation =
fail "operation failed"

secureOperation : ConcurrentTask String ()
secureOperation =
lockResource
|> andThenDo performOperation
|> finallyDo unlockResource

-}
finallyDo : ConcurrentTask x a -> ConcurrentTask x b -> ConcurrentTask x a
finallyDo t2 t1 =
t1
|> onError (\e -> t2 |> andThenDo (fail e))
|> andThenDo t2



-- Batch Helpers

Expand Down