logo

tcp

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

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

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

    return a / b;
  }

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

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