logo

mtc

The mtc function stands for make try catch. It takes a function and returns a function that will return the result of the original function or an error.

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

(async () => {
  let divide;

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

    return a / b;
  });

  // will log 1.5
  console.log(divide(3, 2));

  // will log Error
  console.log(divide(1, 0));
})();