#Function: fromResult()
Converts a Result from
@praha/byethrow into an
Option or OptionAsync.
A Success becomes a Some; a Failure becomes a None, discarding the error.
#Type Param
T
The success value type.
#Examples
Success Case
import { import Option Option } from 'byenull';
import { import Result Result } from '@praha/byethrow';
const const option: Option.Option<2> option = import Option Option .function fromResult<2>(result: Result<2, any>): Option.Option<2> (+1 overload)Converts a Result from
https://github.com/praha-inc/byethrow @praha/byethrow
into an
Option
or
OptionAsync
.
A Success becomes a
Some
; a Failure becomes a
None
, discarding the error.
@typeParamT - The success value type.@exampleSuccess Case
import { Option } from 'byenull';
import { Result } from '@praha/byethrow';
const option = Option.fromResult(Result.succeed(2));
// { type: 'Some', value: 2 }
@exampleFailure Case
import { Option } from 'byenull';
import { Result } from '@praha/byethrow';
const option = Option.fromResult(Result.fail(new Error('Something went wrong')));
// { type: 'None' }
@seetoResult - For the opposite direction.@categoryInterop fromResult (import Result Result .function succeed<2>(value: 2): Result.Result<2, 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 (2));
// { type: 'Some', value: 2 }Failure Case
import { import Option Option } from 'byenull';
import { import Result Result } from '@praha/byethrow';
const const option: Option.Option<never> option = import Option Option .function fromResult<never>(result: Result<never, any>): Option.Option<never> (+1 overload)Converts a Result from
https://github.com/praha-inc/byethrow @praha/byethrow
into an
Option
or
OptionAsync
.
A Success becomes a
Some
; a Failure becomes a
None
, discarding the error.
@typeParamT - The success value type.@exampleSuccess Case
import { Option } from 'byenull';
import { Result } from '@praha/byethrow';
const option = Option.fromResult(Result.succeed(2));
// { type: 'Some', value: 2 }
@exampleFailure Case
import { Option } from 'byenull';
import { Result } from '@praha/byethrow';
const option = Option.fromResult(Result.fail(new Error('Something went wrong')));
// { type: 'None' }
@seetoResult - For the opposite direction.@categoryInterop fromResult (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 (new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error ('Something went wrong')));
// { type: 'None' }#See
toResult - For the opposite direction.
#Call Signature
fromResult<
T>(result):Option<T>
#Type Parameters
#T
T
#Parameters
#result
Result<T, any>
#Returns
Option<T>
#Call Signature
fromResult<
T>(result):OptionAsync<T>
#Type Parameters
#T
T
#Parameters
#result
ResultAsync<T, any>
#Returns
OptionAsync<T>