
class Result {
    constructor(value, error) {
        this.value = value;
        this.error = error;
    }

    static success(value) {
        return new Result(value, null);
    }

    static failure(error) {
        return new Result(null, error);
    }

    map(fn) {
        if (this.error) return this;
        try {
            return Result.success(fn(this.value));
        } catch (e) {
            return Result.failure(e);
        }
    }
}

const asyncHandler = (fn) => async (...args) => {
    try {
        const result = await fn(...args);
        return Result.success(result);
    } catch (error) {
        return Result.failure(error);
    }
};

export { asyncHandler, Result };