Effect CommunityEC
Effect Community2y ago
11 replies
addamsson

Managing Resources in TypeScript with S3: Handling Bucket Creation and Deletion

I'm looking at the "Resource Management" chapter of the docs and I was wondering about this piece of code:
const createBucket = S3.pipe(
    Effect.flatMap(({ createBucket, deleteBucket }) =>
        Effect.acquireRelease(createBucket, (bucket, exit) =>
            Exit.isFailure(exit) ? deleteBucket(bucket) : Effect.unit
        )
    )
);

what can I do if deleteBucket can also fail? how do I recover from that using acquireRelease?
Here, deleteBucket has no error case:
export interface S3 {
    createBucket: Effect.Effect<never, S3Error, Bucket>;
    deleteBucket: (bucket: Bucket) => Effect.Effect<never, never, void>;
}

what if it can also give back an S3Error? (for example I was able to create the bucket, but there is a connection error and deleteBucket fails.
Was this page helpful?