Skip to content

[feature] finallyDo #33

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

Closed
ni-ko-o-kin opened this issue Mar 26, 2024 · 3 comments
Closed

[feature] finallyDo #33

ni-ko-o-kin opened this issue Mar 26, 2024 · 3 comments

Comments

@ni-ko-o-kin
Copy link

ni-ko-o-kin commented Mar 26, 2024

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.

@andrewMacmurray
Copy link
Owner

Sorry missed this - sure, that feels like it would fit sensibly in the helpers section, feel free to make a PR.

@ni-ko-o-kin
Copy link
Author

Thanks! WIP!

@ni-ko-o-kin
Copy link
Author

see #34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants