logo

mtcp

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

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

(async () => {
  let divide;

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

    return a / b;
  });

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

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