Effect CommunityEC
Effect Community•3y ago•
24 replies
Guilty Pleasure

Improving File Existence Handling in Effect

Hello everyone 👋 I'm new to Effect (and fp) and was wondering if there are better ways of handling this snippet (I'm testing this is bun as well)
export function doesFileExist(inputFile: BunFile) {
    return pipe(
        inputFile,
        file => Effect.tryPromise({
            try: () => file.exists(),
            catch: () => new FileNotFound()
        }),
        Effect.flatMap(v => {
            return v
                // Return the input file of the parent function
                ? Effect.succeed(inputFile)
                : Effect.fail(new FileNotFound())
        })
    )
}

I want to add a little check in my main pipe, which looks like pipe(file, doesFileExist, readFile), but it feels like there should be a better way to achieve this without overriding the boolean value
Was this page helpful?