Skip to content

[feature] finallyDo #33

@ni-ko-o-kin

Description

@ni-ko-o-kin
import ConcurrentTask as CT exposing (ConcurrentTask)

finallyDo : ConcurrentTask e a -> ConcurrentTask e b -> ConcurrentTask e a
finallyDo secondTask firstTask =
    firstTask
        |> CT.onError (\e -> secondTask |> CT.andThenDo (CT.fail e))
        |> CT.andThenDo secondTask

Similar to Javascripts try-catch-finally statement. For example, I'm using finallyDo to make sure unlockResource gets called in both, the success and failure case:

callInsideLock : ConcurrentTask e a -> ConcurrentTask e b
callInsideLock task =
    lockResource
        |> CT.andThenDo task
        |> CT.finallyDo unlockResource

instead of

callInsideLock : ConcurrentTask e a -> ConcurrentTask e b
callInsideLock task =
     lockResource
        |> CT.andThenDo (task |> CT.onError (\e -> CT.andThenDo (CT.fail e) unlockResource))
        |> CT.andThenDo unlockResource

Are you interested in adding this to elm-concurrent-task? I would open a PR if so.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions