Asyncnullabletrycatch

asyncNullableTryCatch

asyncNullableTryCatch asynchronously runs the function passed to it and returns its return value if everything goes right. If an error is thrown, it returns null

Import

import { asyncNullableTryCatch } from '@fullstacksjs/toolbox';

Signature

async function asyncNullableTryCatch<T>(
  fn: (...args: any[]) => Promise<T>,
): Promise<T | null>;

Examples

If there are no errors:

const execute = () => Promise.resolve('ok');
 
await asyncNullableTryCatch(execute); // 'ok'

If there is an error:

const execute = () => Promise.reject('Unknown Error');
 
await asyncNullableTryCatch(execute); // null