R
fn

tryIt

v0.0.3tested

Wraps promise-based code in a try/catch block without forking the control flow

Examples

ts
const wrappedFetch = tryIt(fetch);
const { error, data } = await wrappedFetch('https://jsonplaceholder.typicode.com/todos/1');
ts
const { error, data } = await tryIt(fetch)('https://jsonplaceholder.typicode.com/todos/1');

Signature

ts
export function tryIt<Args extends any[], Return>(
  fn: (...args: Args) => Return,
){ ... }

Type Parameters

Argsextends any[]
Return

Parameters

ParameterTypeDescription
fn(...args: Args) => ReturnThe function to try

Returns

(...args: Args) => TryItReturn<Return>- The function that will return a tuple with the error and the result