#デバッグ
パイプラインが期待どおりに動かないときは、各ステップの値を確認したくなります。
inspect と inspectError を使うと、パイプラインを流れる Result に影響を与えずに、ログ出力などの副作用を実行できます。
#inspect で成功値を確認する
inspect(fn)ケースを選ぶと、その流れだけを追えますinspect は成功値を引数に渡された関数を呼び出し、元の Result を変更せずに返します。
Result が Failure の場合、関数は呼び出されません。
import { import Result Result } from '@praha/byethrow';
const const success: Result.Result<number, never> success = import Result Result .function pipe<Result.Result<42, never>, Result.Result<42, never>, Result.Result<number, never>>(a: Result.Result<42, never>, ab: (a: Result.Result<42, never>) => Result.Result<42, never>, bc: (b: Result.Result<42, never>) => Result.Result<number, never>): Result.Result<number, 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<42>(value: 42): Result.Result<42, 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 (42),
import Result Result .function inspect<Result.Result<42, never>, void>(fn: (a: 42) => void): (result: Result.Result<42, never>) => Result.Result<42, never> (+1 overload)Executes a side effect function on the success value of a
Result
or
ResultAsync
,
without modifying the original result. This is useful for debugging, logging, or performing
other side effects while maintaining the original value and error state.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The return type of the inspection function.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.inspect((value) => console.log('Debug:', value)),
);
// Console output: "Debug: 42"
// Result: { type: 'Success', value: 42 }
@exampleFailure Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.inspect((value) => console.log('Debug:', value)),
);
// No console output
// Result: { type: 'Failure', error: 'error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspect ((value: 42 value ) => var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log ('Value:', value: 42 value )),
import Result Result .function map<Result.Result<42, never>, number>(fn: (a: 42) => number): (result: Result.Result<42, never>) => Result.Result<number, never> (+1 overload)Applies a transformation function to the success value of a
Result
or
ResultAsync
.
If the input is a
Failure
, it will be returned unchanged.
@typeParamR1 - The input Result or ResultAsync .@typeParamT2 - The transformed success value type.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(2),
Result.map((x) => x * 10),
);
// { type: 'Success', value: 20 }
@exampleFailure Case (unchanged)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.map((x) => x * 10),
);
// { type: 'Failure', error: 'error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators map ((value: 42 value ) => value: 42 value * 2),
);
// コンソール: "Value: 42"
// { type: 'Success', value: 84 }
const const failure: Result.Result<never, "error"> failure = 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 inspect<Result.Result<never, "error">, void>(fn: (a: never) => void): (result: Result.Result<never, "error">) => Result.Result<never, "error"> (+1 overload)Executes a side effect function on the success value of a
Result
or
ResultAsync
,
without modifying the original result. This is useful for debugging, logging, or performing
other side effects while maintaining the original value and error state.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The return type of the inspection function.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.inspect((value) => console.log('Debug:', value)),
);
// Console output: "Debug: 42"
// Result: { type: 'Success', value: 42 }
@exampleFailure Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.inspect((value) => console.log('Debug:', value)),
);
// No console output
// Result: { type: 'Failure', error: 'error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspect ((value: never value ) => var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log ('Value:', value: never value )), // 呼び出されない
);
// { type: 'Failure', error: 'error' }#inspectError でエラーを確認する
inspectError(fn)ケースを選ぶと、その流れだけを追えますinspectError は、エラーに対する inspect の対になる関数です。
エラー値を引数に渡された関数を呼び出し、Success の場合は何もしません。
import { import Result Result } from '@praha/byethrow';
const const failure: Result.Result<never, "Something went wrong"> failure = import Result Result .function pipe<Result.Result<never, "Something went wrong">, Result.Result<never, "Something went wrong">>(a: Result.Result<never, "Something went wrong">, ab: (a: Result.Result<never, "Something went wrong">) => Result.Result<never, "Something went wrong">): Result.Result<never, "Something went wrong"> (+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<"Something went wrong">(error: "Something went wrong"): Result.Result<never, "Something went wrong"> (+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 ('Something went wrong'),
import Result Result .function inspectError<Result.Result<never, "Something went wrong">, void>(fn: (a: "Something went wrong") => void): (result: Result.Result<never, "Something went wrong">) => Result.Result<never, "Something went wrong"> (+1 overload)Executes a side effect function on the error value of a
Result
or
ResultAsync
,
without modifying the original result. This is useful for debugging, logging, or performing
other side effects while maintaining the original value and error state.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The return type of the inspection function.@exampleFailure Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.inspectError((error) => console.log('Debug error:', error)),
);
// Console output: "Debug error: error"
// Result: { type: 'Failure', error: 'error' }
@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.inspectError((error) => console.log('Debug error:', error)),
);
// No console output
// Result: { type: 'Success', value: 42 }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspectError ((error: "Something went wrong" error ) => var console: Console console .Console.error(...data: any[]): voidThe console.error() static method outputs a message to the console at the "error" log level. The message is only displayed to the user if the console is configured to display error output. In most cases, the log level is configured within the console UI. The message may be formatted as an error, with red colors and call stack information.
error ('Error:', error: "Something went wrong" error )),
);
// コンソール: "Error: Something went wrong"
// { type: 'Failure', error: 'Something went wrong' }
const const success: Result.Result<42, never> success = import Result Result .function pipe<Result.Result<42, never>, Result.Result<42, never>>(a: Result.Result<42, never>, ab: (a: Result.Result<42, never>) => Result.Result<42, never>): Result.Result<42, 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<42>(value: 42): Result.Result<42, 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 (42),
import Result Result .function inspectError<Result.Result<42, never>, void>(fn: (a: never) => void): (result: Result.Result<42, never>) => Result.Result<42, never> (+1 overload)Executes a side effect function on the error value of a
Result
or
ResultAsync
,
without modifying the original result. This is useful for debugging, logging, or performing
other side effects while maintaining the original value and error state.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The return type of the inspection function.@exampleFailure Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.inspectError((error) => console.log('Debug error:', error)),
);
// Console output: "Debug error: error"
// Result: { type: 'Failure', error: 'error' }
@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.inspectError((error) => console.log('Debug error:', error)),
);
// No console output
// Result: { type: 'Success', value: 42 }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspectError ((error: never error ) => var console: Console console .Console.error(...data: any[]): voidThe console.error() static method outputs a message to the console at the "error" log level. The message is only displayed to the user if the console is configured to display error output. In most cases, the log level is configured within the console UI. The message may be formatted as an error, with red colors and call stack information.
error ('Error:', error: never error )), // 呼び出されない
);
// { type: 'Success', value: 42 }#例:パイプラインを追跡する
観察したいステップの間に inspect を挟み、末尾に inspectError を置くと、どのステップで失敗してもログに残せます。
import { import Result Result } from '@praha/byethrow';
const const processData: (input: Input) => Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed"> processData = (input: Input input : type Input = {
data: string;
}
Input ) => {
return import Result Result .function pipe<Result.Result<Input, never>, Result.Result<Input, never>, Result.Result<Input, "ValidateFailed">, Result.Result<Transformed, "ValidateFailed" | "TransformFailed">, Result.Result<Transformed, "ValidateFailed" | "TransformFailed">, Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed">, Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed">, Result.Result<Saved, "ValidateFailed" | ... 1 more ... | "SaveFailed">>(a: Result.Result<...>, ab: (a: Result.Result<...>) => Result.Result<...>, bc: (b: Result.Result<...>) => Result.Result<...>, cd: (c: Result.Result<...>) => Result.Result<...>, de: (d: Result.Result<...>) => Result.Result<...>, ef: (e: Result.Result<...>) => Result.Result<...>, fg: (f: Result.Result<...>) => Result.Result<...>, gh: (g: 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<Input>(value: Input): Result.Result<Input, 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 (input: Input input ),
import Result Result .function inspect<Result.Result<Input, never>, void>(fn: (a: Input) => void): (result: Result.Result<Input, never>) => Result.Result<Input, never> (+1 overload)Executes a side effect function on the success value of a
Result
or
ResultAsync
,
without modifying the original result. This is useful for debugging, logging, or performing
other side effects while maintaining the original value and error state.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The return type of the inspection function.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.inspect((value) => console.log('Debug:', value)),
);
// Console output: "Debug: 42"
// Result: { type: 'Success', value: 42 }
@exampleFailure Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.inspect((value) => console.log('Debug:', value)),
);
// No console output
// Result: { type: 'Failure', error: 'error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspect ((value: Input value ) => var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log ('Input:', value: Input value )),
import Result Result .function andThrough<Result.Result<Input, never>, Result.Result<void, "ValidateFailed">>(fn: (a: Input) => Result.Result<void, "ValidateFailed">): (result: Result.Result<Input, never>) => Result.Result<Input, "ValidateFailed"> (+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 validate: (input: Input) => Result.Result<void, "ValidateFailed"> validate ),
import Result Result .function andThen<Result.Result<Input, "ValidateFailed">, Result.Result<Transformed, "TransformFailed">>(fn: (a: Input) => Result.Result<Transformed, "TransformFailed">): (result: Result.Result<Input, "ValidateFailed">) => Result.Result<Transformed, "ValidateFailed" | "TransformFailed"> (+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 (const transform: (input: Input) => Result.Result<Transformed, "TransformFailed"> transform ),
import Result Result .function inspect<Result.Result<Transformed, "ValidateFailed" | "TransformFailed">, void>(fn: (a: Transformed) => void): (result: Result.Result<Transformed, "ValidateFailed" | "TransformFailed">) => Result.Result<Transformed, "ValidateFailed" | "TransformFailed"> (+1 overload)Executes a side effect function on the success value of a
Result
or
ResultAsync
,
without modifying the original result. This is useful for debugging, logging, or performing
other side effects while maintaining the original value and error state.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The return type of the inspection function.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.inspect((value) => console.log('Debug:', value)),
);
// Console output: "Debug: 42"
// Result: { type: 'Success', value: 42 }
@exampleFailure Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.inspect((value) => console.log('Debug:', value)),
);
// No console output
// Result: { type: 'Failure', error: 'error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspect ((value: Transformed value ) => var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log ('After transform:', value: Transformed value )),
import Result Result .function andThen<Result.Result<Transformed, "ValidateFailed" | "TransformFailed">, Result.Result<Saved, "SaveFailed">>(fn: (a: Transformed) => Result.Result<Saved, "SaveFailed">): (result: Result.Result<Transformed, "ValidateFailed" | "TransformFailed">) => Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed"> (+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 (const save: (input: Transformed) => Result.Result<Saved, "SaveFailed"> save ),
import Result Result .function inspect<Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed">, void>(fn: (a: Saved) => void): (result: Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed">) => Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed"> (+1 overload)Executes a side effect function on the success value of a
Result
or
ResultAsync
,
without modifying the original result. This is useful for debugging, logging, or performing
other side effects while maintaining the original value and error state.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The return type of the inspection function.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.inspect((value) => console.log('Debug:', value)),
);
// Console output: "Debug: 42"
// Result: { type: 'Success', value: 42 }
@exampleFailure Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.inspect((value) => console.log('Debug:', value)),
);
// No console output
// Result: { type: 'Failure', error: 'error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspect ((value: Saved value ) => var console: Console console .Console.log(...data: any[]): voidThe console.log() static method outputs a message to the console.
log ('After save:', value: Saved value )),
import Result Result .function inspectError<Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed">, void>(fn: (a: "ValidateFailed" | "TransformFailed" | "SaveFailed") => void): (result: Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed">) => Result.Result<Saved, "ValidateFailed" | "TransformFailed" | "SaveFailed"> (+1 overload)Executes a side effect function on the error value of a
Result
or
ResultAsync
,
without modifying the original result. This is useful for debugging, logging, or performing
other side effects while maintaining the original value and error state.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The return type of the inspection function.@exampleFailure Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('error'),
Result.inspectError((error) => console.log('Debug error:', error)),
);
// Console output: "Debug error: error"
// Result: { type: 'Failure', error: 'error' }
@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.inspectError((error) => console.log('Debug error:', error)),
);
// No console output
// Result: { type: 'Success', value: 42 }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators inspectError ((error: "ValidateFailed" | "TransformFailed" | "SaveFailed" error ) => var console: Console console .Console.error(...data: any[]): voidThe console.error() static method outputs a message to the console at the "error" log level. The message is only displayed to the user if the console is configured to display error output. In most cases, the log level is configured within the console UI. The message may be formatted as an error, with red colors and call stack information.
error ('Pipeline failed:', error: "ValidateFailed" | "TransformFailed" | "SaveFailed" error )),
);
};#注意点
- 戻り値は無視されます。 関数が
Failureを返しても、元のResultがそのまま次に渡されます。副作用が失敗する可能性があり、その失敗が重要な場合は、代わりにandThroughやorThroughを使ってください。 - 非同期関数は待機されます。 関数が
Promiseを返すと、パイプラインはその完了を待ってから次に進み、非同期になります。 - デバッグ専用ではありません。 パイプラインの末尾に
inspectとinspectErrorを置くのは、出力の表示や終了コードの設定など、最終的な結果に応じた処理を行うシンプルな方法でもあります。このパターンは実装例を参照してください。
#リファレンス
| API | 用途 |
|---|---|
| inspect(fn) | 成功値に対して副作用を実行する |
| inspectError(fn) | エラー値に対して副作用を実行する |
