Skip to content

Commit

Permalink
FIX: Change comments to JSDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
ahme-dev committed May 18, 2023
1 parent 35e765a commit d66b27c
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// takes in a function, and catches any errors in it
// then returns either the function's returned or the error
/**
*
* takes in a function, and catches any errors in it
* then returns either the function's returned or the error
*
* @param func The function to run, should typically look like ()=>yourfunction(2)
* @returns A promise containing a union of the value and the error
*/
export async function resultAll<T>(func: () => T): Promise<T | Error> {
return new Promise((resolve) => {
try {
Expand All @@ -11,8 +17,14 @@ export async function resultAll<T>(func: () => T): Promise<T | Error> {
});
}

// takes in an async function and catches errors in it
// then returns either the function's returned or the error
/**
*
* Takes in an async function and catches errors in it
* then returns either the function's returned or the error
*
* @param promise Can be a promise or a called async function which returns a promise
* @returns A promise containing a union of the value and the error
*/
export async function resultAsync<T>(promise: Promise<T>): Promise<T | Error> {
try {
const ok = await promise;
Expand All @@ -24,6 +36,15 @@ export async function resultAsync<T>(promise: Promise<T>): Promise<T | Error> {

// checks if a variable is of type Error
// used to type guard against results being errors
export function hasError(err: unknown): err is Error {
return err instanceof Error;

/**
*
* Takes in a value which can possibly be of type error and returns a boolean indicating if it was or not.
* Also it'll act as a type constraint, making the variable change from union type to the value type, in subsequent code.
*
* @param possibleErr Any value which can possibly be an error
* @returns A boolean indicating whether the value was of type error
*/
export function hasError(possibleErr: unknown): possibleErr is Error {
return possibleErr instanceof Error;
}

0 comments on commit d66b27c

Please sign in to comment.