logo

tc

The tc function stands for try catch. It takes a function and returns the result of the function or the error that was thrown.

import { tc } from '@aracna/core';

function divide(a, b) {
  if (b === 0) {
    throw new Error('cannot divide by zero');
  }

  return a / b;
}

// will log 1.5
console.log(tc(() => divide(3, 1)));

// will log Error
console.log(tc(() => divide(1, 0)));