1.0.0 • Published 11 months ago

@zowanet/wrap-catch v1.0.0

Weekly downloads
-
License
Unlicense
Repository
github
Last release
11 months ago

wrap-catch

Install

npm install @zowanet/wrap-catch
yarn add @zowanet/wrap-catch
pnpm add @zowanet/wrap-catch

Import

import wrapCatch from '@zowanet/wrap-catch'; // wrapCatchSync is the default import
import { wrapCatchAsync, wrapCatchSync } from '@zowanet/wrap-catch';
const { default: wrapCatch } = await import('@zowanet/wrap-catch'); // wrapCatchSync is the default import
const { wrapCatchAsync, wrapCatchSync } = await import('@zowanet/wrap-catch');

Usage

TypeScript:

const getSmallNumber: (input: number) => number | Error = wrapCatch((input: number): number => {
    if (input > 0.5) {
        throw new Error('Too big');
    } else {
        return input;
    }
});

const result1: number | Error = getSmallNumber(0.1);
console.log(result1); // 0.1

const result2: number | Error = getSmallNumber(0.9);
console.log(result2); // Error

JavaScript:

const getSmallNumber = wrapCatch(input => {
    if (input > 0.5) {
        throw new Error('Too big');
    } else {
        return input;
    }
});

const result1 = getSmallNumber(0.1);
console.log(result1); // 0.1

const result2 = getSmallNumber(0.9);
console.log(result2); // Error
1.0.0

11 months ago

0.1.0

11 months ago