Functional resource management with Bracket

Note

A problem when acquiring resources for use in applications (opening files, database connections, http connections) is closing them even when the operation fails. Imperative programs have try,catch,finally for this, but what does Cats Effect have in the functional world? It has Bracket. It's a type class that can have an instance for a custom type, it is also defined for IO by default.

Some of its usages are as follows.

The close will run even if any of the other operations fails or succeeds

open(file).bracket { readFile } { close }

Also guarantee is another function like this This is the same idea as try,catch,finally, the otherAction will always run.

action.guarantee(otherAction)

Links

Basic resource management with Bracket in cats-effect