The successful value type
Private
constructorPrivate
Readonly
#errorError when the attempt failed
Private
Readonly
#valueValue when the attempt was successful
Assert against a successful attempt's value
Attempt.ofValue(123)
.assert(value => value > 100, (value) => new Error("Number isn't greater than 100"))
.get() // 123
Attempt.ofValue(123)
.assert(value => value < 100, (value) => new Error(`Number $(value) isn't less than 100`))
.getError() // Error("Number 123 isn't less than 100")
Attempt.ofError<number>("Something went wrong")
.assert(value => value > 100, (value) => new Error("Number isn't greater than 100"))
.getError() // Error("Something went wrong")
Assertion function to run
Function that returns an error if the assertion fails
Either success/failed attempt from the assertion or the failed attempt it was called on
Run success or failure function based on attempt
Attempt.ofValue(123)
.ifElse(
value => console.log(`It worked! ${value}`),
error => console.log(`It failed! ${error}`)
)
Function to run on successful attempt
Function to run on failed attempt
Map a success attempt or return the current failed attempt
Attempt.ofValue(123).map(value => value * 2).get() // 456
Attempt.ofValue(123)
.map(value => {
throw new Error("Something went wrong");
return value;
})
.map(value => value * 2)
.getError() // Error("Something went wrong")
Mapping function
Function to map over attempt
Either mapped success attempt or the current failed attempt
Get value from successful attempt or a throw error
Attempt.ofValue(123).orThrow(456) // 123
Attempt<number>.ofError("...").orThrow("Something went wrong") // Error("Something went wrong")
You can also get the error from a failed attempt
Attempt<number>.ofError("...").orThrow((e) => "Something went wrong: ${e}") // Error("Something went wrong: ...")
Successful attempt value
Static
ofCreate an attempt from a function.
// Attempt<ReturnType<typeof fnThatMightThrow>>
const attempt = Attempt.of(fnThatMightThrow, fnArg1, fnArg2, ...);
Async works too!
// Promise<Attempt<ReturnType<typeof fnThatMightThrow>>>
const attemptPromise = Attempt.of(asyncFnThatMightThrow, fnArg1, fnArg2, ...);
// Attempt<ReturnType<typeof fnThatMightThrow>>
const attempt = await attemptPromise;
This also works with functions that return an Attempt
const value = 123;
const attemptA = Attempt.ofValue(value);
const attemptB = Attempt.of(() => attemptA); // Attempt<number>
attemptB.get(); // 123
Function to be called
Function to run
Rest
...fnArgs: Parameters<Fn>Arguments to pass when calling function
Either a successful or failing attempt
Static
ofCreate a failed attempt result from an error
// Attempt<unknown>
const failedAttemptWithError = Attempt.ofError(new Error("Something went wrong"));
// Attempt<unknown>
const failedAttemptWithString = Attempt.ofError("Something went wrong");
Value type if attempt had succeeded
Error for the failed attempt
Attempt with failure error
Static
ofCreate a successful attempt from a value
// Attempt<number>
const attempt = Attempt.ofValue(123);
Value for the successful attempt
Value of successful attempt
Attempt with a success value
Static
wrapWrap the underlying function to be called as an attempt
function sum(...args: number[]): number {
return args.reduce((acc, curr) => acc + curr, 0);
}
const attemptToSum = Attempt.wrap(sum);
const sumValue = attemptToSum(1, 2, 3)
.orThrow(e => `Unable to sum: {e}`); // 6
Function to wrap
A function that calls the underlying function
Wrap the underlying function to be called as an attempt
function sum(...args: number[]): number {
return args.reduce((acc, curr) => acc + curr, 0);
}
const attemptToSum = Attempt.wrap(sum);
const sumValue = attemptToSum(1, 2, 3)
.orThrow(e => `Unable to sum: {e}`); // 6
Rest
...args: Parameters<Fn>A function that calls the underlying function
Generated using TypeDoc
Typed error handling for calling functions
It also works with async functions!
You can also wrap a function to be called later