#Validation and Cleanup
Sometimes you want to run an additional step that can fail, without replacing the value you already have.
andThrough and orThrough run such a step and, if it succeeds, keep the original Result.
#Running Checks with andThrough
andThrough(fn)Select a case to follow its pathandThrough calls the given function with the success value, just like andThen.
The difference is what happens when the function succeeds:
- If the function returns a
Success, its value is discarded and the originalSuccessis returned. - If the function returns a
Failure, thatFailureis returned. - If the input is a
Failure, the function is not called.
import { import Result Result } from '@praha/byethrow';
const const checkPositive: (value: number) => Result.Success<void> | Result.Failure<"Must be positive"> checkPositive = (value: number value : number) => {
return 0 < value: number value ? import Result Result .function succeed(): Result.Result<void, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed () : import Result Result .function fail<"Must be positive">(error: "Must be positive"): Result.Result<never, "Must be positive"> (+1 overload)Creates a
Failure
result from a given error.
Passing a Promise is allowed, but it is recommended to await it before passing to fail.
@typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.fail('Something went wrong');
// Result.Result<never, string>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.fail();
// Result.Result<never, void>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators fail ('Must be positive');
};
const const passed: Result.Result<5, "Must be positive"> passed = import Result Result .function pipe<Result.Result<5, never>, Result.Result<5, "Must be positive">>(a: Result.Result<5, never>, ab: (a: Result.Result<5, never>) => Result.Result<5, "Must be positive">): Result.Result<5, "Must be positive"> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 26 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Result } from '@praha/byethrow';
const result = Result.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
import Result Result .function succeed<5>(value: 5): Result.Result<5, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (5),
import Result Result .function andThrough<Result.Result<5, never>, Result.Success<void> | Result.Failure<"Must be positive">>(fn: (a: 5) => Result.Success<void> | Result.Failure<"Must be positive">): (result: Result.Result<5, never>) => Result.Result<5, "Must be positive"> (+1 overload)Runs an additional computation using the success value of a
Result
or
ResultAsync
,
but returns the original result if the additional computation is successful.
If either the original result or the side effect result is a
Failure
, that failure is returned.
Useful for running validations or side effects without altering the main result on success.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(5),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Success', value: 5 }
@exampleFailure Case (input is a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Failure', error: 'error' }
@exampleFailure Case (function returns a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(-10),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Failure', error: 'Must be > 0' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators andThrough (const checkPositive: (value: number) => Result.Success<void> | Result.Failure<"Must be positive"> checkPositive ),
);
// { type: 'Success', value: 5 } - the original value is kept
const const rejected: Result.Result<-10, "Must be positive"> rejected = import Result Result .function pipe<Result.Result<-10, never>, Result.Result<-10, "Must be positive">>(a: Result.Result<-10, never>, ab: (a: Result.Result<-10, never>) => Result.Result<-10, "Must be positive">): Result.Result<-10, "Must be positive"> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 26 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Result } from '@praha/byethrow';
const result = Result.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
import Result Result .function succeed<-10>(value: -10): Result.Result<-10, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (-10),
import Result Result .function andThrough<Result.Result<-10, never>, Result.Success<void> | Result.Failure<"Must be positive">>(fn: (a: -10) => Result.Success<void> | Result.Failure<"Must be positive">): (result: Result.Result<-10, never>) => Result.Result<-10, "Must be positive"> (+1 overload)Runs an additional computation using the success value of a
Result
or
ResultAsync
,
but returns the original result if the additional computation is successful.
If either the original result or the side effect result is a
Failure
, that failure is returned.
Useful for running validations or side effects without altering the main result on success.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(5),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Success', value: 5 }
@exampleFailure Case (input is a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Failure', error: 'error' }
@exampleFailure Case (function returns a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(-10),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Failure', error: 'Must be > 0' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators andThrough (const checkPositive: (value: number) => Result.Success<void> | Result.Failure<"Must be positive"> checkPositive ),
);
// { type: 'Failure', error: 'Must be positive' }
const const skipped: Result.Result<never, "Must be positive" | "error"> skipped = import Result Result .function pipe<Result.Result<never, "error">, Result.Result<never, "Must be positive" | "error">>(a: Result.Result<never, "error">, ab: (a: Result.Result<never, "error">) => Result.Result<never, "Must be positive" | "error">): Result.Result<never, "Must be positive" | "error"> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 26 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Result } from '@praha/byethrow';
const result = Result.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
import Result Result .function fail<"error">(error: "error"): Result.Result<never, "error"> (+1 overload)Creates a
Failure
result from a given error.
Passing a Promise is allowed, but it is recommended to await it before passing to fail.
@typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.fail('Something went wrong');
// Result.Result<never, string>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.fail();
// Result.Result<never, void>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators fail ('error'),
import Result Result .function andThrough<Result.Result<never, "error">, Result.Success<void> | Result.Failure<"Must be positive">>(fn: (a: never) => Result.Success<void> | Result.Failure<"Must be positive">): (result: Result.Result<never, "error">) => Result.Result<never, "Must be positive" | "error"> (+1 overload)Runs an additional computation using the success value of a
Result
or
ResultAsync
,
but returns the original result if the additional computation is successful.
If either the original result or the side effect result is a
Failure
, that failure is returned.
Useful for running validations or side effects without altering the main result on success.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(5),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Success', value: 5 }
@exampleFailure Case (input is a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Failure', error: 'error' }
@exampleFailure Case (function returns a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(-10),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Failure', error: 'Must be > 0' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators andThrough (const checkPositive: (value: number) => Result.Success<void> | Result.Failure<"Must be positive"> checkPositive ), // Not called
);
// { type: 'Failure', error: 'error' }This makes andThrough a good fit for validations and for side effects that can fail, such as writing an audit log, where the next step still needs the original value.
#Example: Multiple Validations
Each validation only needs to report whether the value is acceptable, so it can simply return Result.Result<void, E>:
import { import Result Result } from '@praha/byethrow';
type type User = {
name: string;
age: number;
email: string;
}
User = { name: string name : string; age: number age : number; email: string email : string };
declare const const validateName: (user: User) => Result.Result<void, "InvalidName"> validateName : (user: User user : type User = {
name: string;
age: number;
email: string;
}
User ) => import Result Result .type Result<T, E> = Result.Success<T> | Result.Failure<E>A union type representing either a success or a failure.
@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@exampleimport { Result } from '@praha/byethrow';
const doSomething = (): Result.Result<number, string> => {
return Math.random() > 0.5
? { type: 'Success', value: 10 }
: { type: 'Failure', error: 'Oops' };
};
@categoryCore Types Result <void, 'InvalidName'>;
declare const const validateAge: (user: User) => Result.Result<void, "InvalidAge"> validateAge : (user: User user : type User = {
name: string;
age: number;
email: string;
}
User ) => import Result Result .type Result<T, E> = Result.Success<T> | Result.Failure<E>A union type representing either a success or a failure.
@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@exampleimport { Result } from '@praha/byethrow';
const doSomething = (): Result.Result<number, string> => {
return Math.random() > 0.5
? { type: 'Success', value: 10 }
: { type: 'Failure', error: 'Oops' };
};
@categoryCore Types Result <void, 'InvalidAge'>;
declare const const validateEmail: (user: User) => Result.Result<void, "InvalidEmail"> validateEmail : (user: User user : type User = {
name: string;
age: number;
email: string;
}
User ) => import Result Result .type Result<T, E> = Result.Success<T> | Result.Failure<E>A union type representing either a success or a failure.
@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@exampleimport { Result } from '@praha/byethrow';
const doSomething = (): Result.Result<number, string> => {
return Math.random() > 0.5
? { type: 'Success', value: 10 }
: { type: 'Failure', error: 'Oops' };
};
@categoryCore Types Result <void, 'InvalidEmail'>;
const const validateUser: (user: User) => Result.Result<User, "InvalidName" | "InvalidAge" | "InvalidEmail"> validateUser = (user: User user : type User = {
name: string;
age: number;
email: string;
}
User ) => import Result Result .function pipe<Result.Result<User, never>, Result.Result<User, "InvalidName">, Result.Result<User, "InvalidName" | "InvalidAge">, Result.Result<User, "InvalidName" | "InvalidAge" | "InvalidEmail">>(a: Result.Result<User, never>, ab: (a: Result.Result<User, never>) => Result.Result<User, "InvalidName">, bc: (b: Result.Result<User, "InvalidName">) => Result.Result<User, "InvalidName" | "InvalidAge">, cd: (c: Result.Result<...>) => Result.Result<...>): Result.Result<...> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 26 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Result } from '@praha/byethrow';
const result = Result.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
import Result Result .function succeed<User>(value: User): Result.Result<User, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (user: User user ),
import Result Result .function andThrough<Result.Result<User, never>, Result.Result<void, "InvalidName">>(fn: (a: User) => Result.Result<void, "InvalidName">): (result: Result.Result<User, never>) => Result.Result<User, "InvalidName"> (+1 overload)Runs an additional computation using the success value of a
Result
or
ResultAsync
,
but returns the original result if the additional computation is successful.
If either the original result or the side effect result is a
Failure
, that failure is returned.
Useful for running validations or side effects without altering the main result on success.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(5),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Success', value: 5 }
@exampleFailure Case (input is a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Failure', error: 'error' }
@exampleFailure Case (function returns a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(-10),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Failure', error: 'Must be > 0' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators andThrough (const validateName: (user: User) => Result.Result<void, "InvalidName"> validateName ),
import Result Result .function andThrough<Result.Result<User, "InvalidName">, Result.Result<void, "InvalidAge">>(fn: (a: User) => Result.Result<void, "InvalidAge">): (result: Result.Result<User, "InvalidName">) => Result.Result<User, "InvalidName" | "InvalidAge"> (+1 overload)Runs an additional computation using the success value of a
Result
or
ResultAsync
,
but returns the original result if the additional computation is successful.
If either the original result or the side effect result is a
Failure
, that failure is returned.
Useful for running validations or side effects without altering the main result on success.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(5),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Success', value: 5 }
@exampleFailure Case (input is a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Failure', error: 'error' }
@exampleFailure Case (function returns a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(-10),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Failure', error: 'Must be > 0' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators andThrough (const validateAge: (user: User) => Result.Result<void, "InvalidAge"> validateAge ),
import Result Result .function andThrough<Result.Result<User, "InvalidName" | "InvalidAge">, Result.Result<void, "InvalidEmail">>(fn: (a: User) => Result.Result<void, "InvalidEmail">): (result: Result.Result<User, "InvalidName" | "InvalidAge">) => Result.Result<User, "InvalidName" | "InvalidAge" | "InvalidEmail"> (+1 overload)Runs an additional computation using the success value of a
Result
or
ResultAsync
,
but returns the original result if the additional computation is successful.
If either the original result or the side effect result is a
Failure
, that failure is returned.
Useful for running validations or side effects without altering the main result on success.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(5),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Success', value: 5 }
@exampleFailure Case (input is a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Failure', error: 'error' }
@exampleFailure Case (function returns a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(-10),
Result.andThrough((x) => {
return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
}),
);
// { type: 'Failure', error: 'Must be > 0' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators andThrough (const validateEmail: (user: User) => Result.Result<void, "InvalidEmail"> validateEmail ),
);
// Type: Result.Result<User, 'InvalidName' | 'InvalidAge' | 'InvalidEmail'>
// Returns the original user if every validation passes, otherwise the first error
andThrough stops at the first failed validation.
To report every validation error at once, use collect instead.
#Cleaning Up with orThrough
orThrough(fn)Select a case to follow its pathorThrough is the counterpart of andThrough for failures.
It calls the given function with the error value, typically to clean up after a failed operation:
- If the function returns a
Success, the originalFailureis returned. - If the function returns a
Failure, that newFailureis returned. - If the input is a
Success, the function is not called.
import { import Result Result } from '@praha/byethrow';
const const cleanedUp: Result.Result<never, "original error"> cleanedUp = import Result Result .function pipe<Result.Result<never, "original error">, Result.Result<never, "original error">>(a: Result.Result<never, "original error">, ab: (a: Result.Result<never, "original error">) => Result.Result<never, "original error">): Result.Result<never, "original error"> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 26 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Result } from '@praha/byethrow';
const result = Result.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
import Result Result .function fail<"original error">(error: "original error"): Result.Result<never, "original error"> (+1 overload)Creates a
Failure
result from a given error.
Passing a Promise is allowed, but it is recommended to await it before passing to fail.
@typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.fail('Something went wrong');
// Result.Result<never, string>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.fail();
// Result.Result<never, void>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators fail ('original error'),
import Result Result .function orThrough<Result.Result<never, "original error">, Result.Result<void, never>>(fn: (a: "original error") => Result.Result<void, never>): (result: Result.Result<never, "original error">) => Result.Result<never, "original error"> (+1 overload)Runs an additional computation using the error value of a
Result
or
ResultAsync
,
but returns the original failure if the additional computation is successful.
If the original result is a
Success
, it is returned immediately without running the function.
If the original result is a
Failure
, the function is executed with the error value.
If the function returns a
Success
, the original failure is returned.
If the function returns a
Failure
, that new failure is returned.
Useful for running error recovery or fallback logic while preserving the original error on success.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(5),
Result.orThrough((error) => {
return Result.succeed(null);
}),
);
// { type: 'Success', value: 5 }
@exampleFailure Case (function returns a Success)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.orThrough((error) => {
console.log('Logging error:', error);
return Result.succeed(null);
}),
);
// { type: 'Failure', error: 'error' }
@exampleFailure Case (function returns a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('original error'),
Result.orThrough((error) => {
return Result.fail('new error');
}),
);
// { type: 'Failure', error: 'new error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators orThrough (() => import Result Result .function succeed(): Result.Result<void, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed ()),
);
// { type: 'Failure', error: 'original error' } - the original error is kept
const const cleanupFailed: Result.Result<never, "original error" | "cleanup error"> cleanupFailed = import Result Result .function pipe<Result.Result<never, "original error">, Result.Result<never, "original error" | "cleanup error">>(a: Result.Result<never, "original error">, ab: (a: Result.Result<never, "original error">) => Result.Result<never, "original error" | "cleanup error">): Result.Result<never, "original error" | "cleanup error"> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 26 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Result } from '@praha/byethrow';
const result = Result.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
import Result Result .function fail<"original error">(error: "original error"): Result.Result<never, "original error"> (+1 overload)Creates a
Failure
result from a given error.
Passing a Promise is allowed, but it is recommended to await it before passing to fail.
@typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.fail('Something went wrong');
// Result.Result<never, string>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.fail();
// Result.Result<never, void>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators fail ('original error'),
import Result Result .function orThrough<Result.Result<never, "original error">, Result.Result<never, "cleanup error">>(fn: (a: "original error") => Result.Result<never, "cleanup error">): (result: Result.Result<never, "original error">) => Result.Result<never, "original error" | "cleanup error"> (+1 overload)Runs an additional computation using the error value of a
Result
or
ResultAsync
,
but returns the original failure if the additional computation is successful.
If the original result is a
Success
, it is returned immediately without running the function.
If the original result is a
Failure
, the function is executed with the error value.
If the function returns a
Success
, the original failure is returned.
If the function returns a
Failure
, that new failure is returned.
Useful for running error recovery or fallback logic while preserving the original error on success.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(5),
Result.orThrough((error) => {
return Result.succeed(null);
}),
);
// { type: 'Success', value: 5 }
@exampleFailure Case (function returns a Success)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.orThrough((error) => {
console.log('Logging error:', error);
return Result.succeed(null);
}),
);
// { type: 'Failure', error: 'error' }
@exampleFailure Case (function returns a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('original error'),
Result.orThrough((error) => {
return Result.fail('new error');
}),
);
// { type: 'Failure', error: 'new error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators orThrough (() => import Result Result .function fail<"cleanup error">(error: "cleanup error"): Result.Result<never, "cleanup error"> (+1 overload)Creates a
Failure
result from a given error.
Passing a Promise is allowed, but it is recommended to await it before passing to fail.
@typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.fail('Something went wrong');
// Result.Result<never, string>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.fail();
// Result.Result<never, void>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators fail ('cleanup error')),
);
// { type: 'Failure', error: 'cleanup error' }
const const skipped: Result.Result<5, never> skipped = import Result Result .function pipe<Result.Result<5, never>, Result.Result<5, never>>(a: Result.Result<5, never>, ab: (a: Result.Result<5, never>) => Result.Result<5, never>): Result.Result<5, never> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 26 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Result } from '@praha/byethrow';
const result = Result.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
import Result Result .function succeed<5>(value: 5): Result.Result<5, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (5),
import Result Result .function orThrough<Result.Result<5, never>, Result.Result<void, never>>(fn: (a: never) => Result.Result<void, never>): (result: Result.Result<5, never>) => Result.Result<5, never> (+1 overload)Runs an additional computation using the error value of a
Result
or
ResultAsync
,
but returns the original failure if the additional computation is successful.
If the original result is a
Success
, it is returned immediately without running the function.
If the original result is a
Failure
, the function is executed with the error value.
If the function returns a
Success
, the original failure is returned.
If the function returns a
Failure
, that new failure is returned.
Useful for running error recovery or fallback logic while preserving the original error on success.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(5),
Result.orThrough((error) => {
return Result.succeed(null);
}),
);
// { type: 'Success', value: 5 }
@exampleFailure Case (function returns a Success)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.orThrough((error) => {
console.log('Logging error:', error);
return Result.succeed(null);
}),
);
// { type: 'Failure', error: 'error' }
@exampleFailure Case (function returns a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('original error'),
Result.orThrough((error) => {
return Result.fail('new error');
}),
);
// { type: 'Failure', error: 'new error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators orThrough (() => import Result Result .function succeed(): Result.Result<void, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed ()), // Not called
);
// { type: 'Success', value: 5 }Unlike orElse, orThrough never turns a failure into a success.
It is meant for work that has to happen because of a failure, while the failure itself is still reported.
#Example: Rolling Back on Failure
Suppose a post is created in two steps: uploading its content, then saving a database record.
If saving the record fails, the uploaded file should be deleted, but the caller should still receive the SavePostFailed error:
import { import Result Result } from '@praha/byethrow';
declare const const uploadFile: (path: string, content: string) => Result.ResultAsync<void, "UploadFailed"> uploadFile : (path: string path : string, content: string content : string) => import Result Result .type ResultAsync<T, E> = Promise<Result.Result<T, E>>An asynchronous variant of
Result
, wrapped in a Promise.
@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@exampleimport { Result } from '@praha/byethrow';
const fetchData = async (): Result.ResultAsync<string, Error> => {
try {
const data = await fetch('...');
return { type: 'Success', value: await data.text() };
} catch (err) {
return { type: 'Failure', error: err as Error };
}
};
@categoryCore Types ResultAsync <void, 'UploadFailed'>;
declare const const savePost: (id: string, path: string) => Result.ResultAsync<void, "SavePostFailed"> savePost : (id: string id : string, path: string path : string) => import Result Result .type ResultAsync<T, E> = Promise<Result.Result<T, E>>An asynchronous variant of
Result
, wrapped in a Promise.
@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@exampleimport { Result } from '@praha/byethrow';
const fetchData = async (): Result.ResultAsync<string, Error> => {
try {
const data = await fetch('...');
return { type: 'Success', value: await data.text() };
} catch (err) {
return { type: 'Failure', error: err as Error };
}
};
@categoryCore Types ResultAsync <void, 'SavePostFailed'>;
declare const const deleteFile: (path: string) => Result.ResultAsync<void, "DeleteFileFailed"> deleteFile : (path: string path : string) => import Result Result .type ResultAsync<T, E> = Promise<Result.Result<T, E>>An asynchronous variant of
Result
, wrapped in a Promise.
@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@exampleimport { Result } from '@praha/byethrow';
const fetchData = async (): Result.ResultAsync<string, Error> => {
try {
const data = await fetch('...');
return { type: 'Success', value: await data.text() };
} catch (err) {
return { type: 'Failure', error: err as Error };
}
};
@categoryCore Types ResultAsync <void, 'DeleteFileFailed'>;
const const createPost: (id: string, content: string) => Result.ResultAsync<void, "UploadFailed" | "SavePostFailed" | "DeleteFileFailed"> createPost = (id: string id : string, content: string content : string) => {
const const path: string path = `/posts/${id: string id }`;
return import Result Result .function pipe<Result.ResultAsync<void, "UploadFailed">, Result.ResultAsync<void, "UploadFailed" | "SavePostFailed" | "DeleteFileFailed">>(a: Result.ResultAsync<void, "UploadFailed">, ab: (a: Result.ResultAsync<void, "UploadFailed">) => Result.ResultAsync<void, "UploadFailed" | "SavePostFailed" | "DeleteFileFailed">): Result.ResultAsync<void, "UploadFailed" | "SavePostFailed" | "DeleteFileFailed"> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 26 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Result } from '@praha/byethrow';
const result = Result.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
const uploadFile: (path: string, content: string) => Result.ResultAsync<void, "UploadFailed"> uploadFile (const path: string path , content: string content ),
import Result Result .function andThen<Result.ResultAsync<void, "UploadFailed">, Result.ResultAsync<void, "SavePostFailed" | "DeleteFileFailed">>(fn: (a: void) => Result.ResultAsync<void, "SavePostFailed" | "DeleteFileFailed">): (result: Result.ResultAsync<void, "UploadFailed">) => Result.ResultAsync<void, "UploadFailed" | "SavePostFailed" | "DeleteFileFailed"> (+1 overload)Chains the next computation using the success value of a
Result
or
ResultAsync
.
If the original result is a
Failure
, it is returned unchanged.
Otherwise, the provided function is called, and its result is returned as-is.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(3),
Result.andThen((x) => Result.succeed(x * 2)),
);
// { type: 'Success', value: 6 }
@exampleFailure Case (input is a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.andThen((x) => Result.succeed(x * 2)),
);
// result: { type: 'Failure', error: 'error' }
@exampleFailure Case (function returns a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(3),
Result.andThen((x) => Result.fail('error: ' + x)),
);
// result: { type: 'Failure', error: 'error: 3' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators andThen (() => import Result Result .function pipe<Result.ResultAsync<void, "SavePostFailed">, Result.ResultAsync<void, "SavePostFailed" | "DeleteFileFailed">>(a: Result.ResultAsync<void, "SavePostFailed">, ab: (a: Result.ResultAsync<void, "SavePostFailed">) => Result.ResultAsync<void, "SavePostFailed" | "DeleteFileFailed">): Result.ResultAsync<void, "SavePostFailed" | "DeleteFileFailed"> (+25 overloads)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 26 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Result } from '@praha/byethrow';
const result = Result.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
const savePost: (id: string, path: string) => Result.ResultAsync<void, "SavePostFailed"> savePost (id: string id , const path: string path ),
// Runs only when savePost fails
import Result Result .function orThrough<Result.ResultAsync<void, "SavePostFailed">, Result.ResultAsync<void, "DeleteFileFailed">>(fn: (a: "SavePostFailed") => Result.ResultAsync<void, "DeleteFileFailed">): (result: Result.ResultAsync<void, "SavePostFailed">) => Result.ResultAsync<void, "SavePostFailed" | "DeleteFileFailed"> (+1 overload)Runs an additional computation using the error value of a
Result
or
ResultAsync
,
but returns the original failure if the additional computation is successful.
If the original result is a
Success
, it is returned immediately without running the function.
If the original result is a
Failure
, the function is executed with the error value.
If the function returns a
Success
, the original failure is returned.
If the function returns a
Failure
, that new failure is returned.
Useful for running error recovery or fallback logic while preserving the original error on success.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(5),
Result.orThrough((error) => {
return Result.succeed(null);
}),
);
// { type: 'Success', value: 5 }
@exampleFailure Case (function returns a Success)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.orThrough((error) => {
console.log('Logging error:', error);
return Result.succeed(null);
}),
);
// { type: 'Failure', error: 'error' }
@exampleFailure Case (function returns a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('original error'),
Result.orThrough((error) => {
return Result.fail('new error');
}),
);
// { type: 'Failure', error: 'new error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators orThrough (() => const deleteFile: (path: string) => Result.ResultAsync<void, "DeleteFileFailed"> deleteFile (const path: string path )),
)),
);
};
// Type: Result.ResultAsync<void, 'UploadFailed' | 'SavePostFailed' | 'DeleteFileFailed'>orThrough is placed right after savePost, so the cleanup runs only when saving fails, not when the upload itself fails.
#References
| API | Purpose |
|---|---|
| andThrough(fn) | Run a step on success while keeping the original value |
| orThrough(fn) | Run a step on failure while keeping the original error |
