Type Alias Result<TValue, TError>

The Result type represents either success or failure.

Type Parameters

  • TValue = unknown
  • TError = unknown
import { Result, resultFailure, resultSuccess, RESULT } from "@daiso-tech/core/utilities";

function random(): Result<string, Error> {
const nbr = Math.round(Math.random() * 100);
if (nbr > 50) {
// The resultFailure function return a failed result
return resultFailure(new Error("Unexpected error occured"));
}
// The resultSuccess function return a success result
return resultSuccess("Function succeded");
}

const result = random();

// Checking for failure
if (result.type === RESULT.FAILURE) {
console.log("Error occured:", result.error);
}

// Checking for success
if (result.type === RESULT.SUCCESS) {
console.log("Result was successful:", result.value);
}

IMPORT_PATH: "@daiso-tech/core/utilities"