#Function: orThrough()
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.
orThrough(fn)Select a case to follow its path#Type Param
R1
The input Result or ResultAsync.
#Type Param
R2
The result type returned by fn.
#Examples
Success Case
import { import Result Result } from '@praha/byethrow';
const const result: Result.Result<5, never> result = 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<null, never>>(fn: (a: never) => Result.Result<null, 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 ((error: never error ) => {
return import Result Result .function succeed<null>(value: null): Result.Result<null, 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 (null);
}),
);
// { type: 'Success', value: 5 }Failure Case (function returns a Success)
import { import Result Result } from '@praha/byethrow';
const const result: Result.Result<never, "error"> result = import Result Result .function pipe<Result.Result<never, "error">, Result.Result<never, "error">>(a: Result.Result<never, "error">, ab: (a: Result.Result<never, "error">) => Result.Result<never, "error">): Result.Result<never, "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 orThrough<Result.Result<never, "error">, Result.Result<null, never>>(fn: (a: "error") => Result.Result<null, never>): (result: Result.Result<never, "error">) => Result.Result<never, "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 ((error: "error" error ) => {
var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log ('Logging error:', error: "error" error );
return import Result Result .function succeed<null>(value: null): Result.Result<null, 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 (null);
}),
);
// { type: 'Failure', error: 'error' }Failure Case (function returns a Failure)
import { import Result Result } from '@praha/byethrow';
const const result: Result.Result<never, "original error" | "new error"> result = import Result Result .function pipe<Result.Result<never, "original error">, Result.Result<never, "original error" | "new error">>(a: Result.Result<never, "original error">, ab: (a: Result.Result<never, "original error">) => Result.Result<never, "original error" | "new error">): Result.Result<never, "original error" | "new 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, "new error">>(fn: (a: "original error") => Result.Result<never, "new error">): (result: Result.Result<never, "original error">) => Result.Result<never, "original error" | "new 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 ((error: "original error" error ) => {
return import Result Result .function fail<"new error">(error: "new error"): Result.Result<never, "new 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 ('new error');
}),
);
// { type: 'Failure', error: 'new error' }#See
pipe - It is recommended to use this function with the pipe function for better readability and composability.
#Call Signature
orThrough<
R1,R2>(fn): (result) =>ResultFor<R1|R2,InferSuccess<R1>,InferFailure<R1> |InferFailure<R2>>
#Type Parameters
#R1
R1 extends ResultMaybeAsync<any, any>
#R2
R2 extends ResultMaybeAsync<any, any>
#Parameters
#fn
(a) => R2
#Returns
(result) => ResultFor<R1 | R2, InferSuccess<R1>, InferFailure<R1> | InferFailure<R2>>
#Call Signature
orThrough<
F>(fn): <R1>(result) =>ResultFor<R1|ReturnType<F>,InferSuccess<R1>,InferFailure<R1> |InferFailure<F>>
#Type Parameters
#F
F extends (a) => ResultMaybeAsync<any, any>
#Parameters
#fn
F
#Returns
<R1>(result) => ResultFor<R1 | ReturnType<F>, InferSuccess<R1>, InferFailure<R1> | InferFailure<F>>
