1.0.0 • Published 1 year ago

tryvial v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

tryvial

Value-packed alternative to try/catch hell

Pronounced like "trivial" (trĭv′ē-əl)

This is a utility library that simplifies the try/catch block and packs on functionality for handling asynchronous operations.

Installation

You can install the package using npm:

npm install tryvial
# or
yarn add tryvial
# or 
pnpm i tryvial

Usage

tryvial tries to simplify code by wrapping the provided function in a try/catch block. If an error is thrown, you can provide an optional fallback(s) or a handler to manage the error. If you enable the retry functionality, the package will attempt to retry the operation a specified number of times. It's also got timeout support, fallbacks, the works.

That's the gist of it, here's the implementation:

import t from "tryvial";

const do_something = async () => {
  // your asynchronous code here
};

const handle_an_error = async (error?: Error) => {
  // your error handling here
}

// Simple implementation
const result = await t(do_something, {
  onError: handle_an_error
})

// Kitchen sink
const result = await t(do_something, {
  retry: true,
  retries: 5,
  retryDelay: 100,
  jitter: 500,
  fallback: [...some_fallbacks],
  timeout: true,
  timeoutAfter: 9999,
  onTimeout: () => console.error("A timeout occured"),
  onRetry: retry => console.log(`Retrying... ${retry}/5`),
  onSuccess: result => console.log(`Success: ${result}`),
  onError: handle_an_error
})

Parameters

tryvial only has two parameters, the fn that it tries, and the options it applies:

nametypedefaultdescription
retrybooleanfalseWhether or not to retry fn
retriesnumber2Number of times to retry before moving to the catch block
retryDelaynumber0Delay (ms) to be implemented between retries
jitternumber0Delay (ms) to be randomly added to retries to prevent network bottlenecking with batch requests. For example, a jitter of 500 will add an additional delay anywhere between 0ms and 500ms.
fallback(() => Promise<T>) \| Array<() => Promise<T>>undefinedFallback function or array of fallback functions to attempt if fn fails. Does not attempt retries. If an array is passed and all fallbacks fail, onError is passed an array of errors (Error[]).
timeoutbooleanfalseWhether or not to enforce a timeout, after which fn automatically fails
timeoutAfternumber10000Time (ms) after which fn will automatically fail
onTimeout() => voidundefinedCallback to run if timeout happens
onRetry(retryNumber: number) => voidundefinedCallback to run if retry happens. Passes the current retry number as argument.
onSuccess(result: T) => voidundefinedCallback to run if fn or fallback succeeds. Passes result as T as argument.
onError(error: Error \| Error[]) => voidundefinedCallback to run if fn or fallback fails. Passes error as Error or error as Error[] as argument.

Contributing

Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT