1.1.8 • Published 1 year ago

try2catch v1.1.8

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Try2Catch

A better try/catch like way to get your errors encapsulated.

Installation

You can install Try2Catch using npm or yarn:

npm install try2catch
yarn add try2catch

Usage

To use Try2Catch, import the Try object from the package:

import { Try } from 'try2catch';

Create a Promise that resolves or rejects after some time, for example:

const awaiter = new Promise((res, reject) => {
    const random = (Math.random() * 10) + 1
    setTimeout(() => { 
            if (random > 5) {
                return res('Nice!');
            }
        
            return reject(new Error('Failed!'));
        }, 5000);
});

An asynchronous function that uses Try to handle the Promise's result:

const processing = async () => {
    const result = await Try.promise(awaiter)

    if (result.isError()) {
        return result.error;
    }

    return result.data
}

In this example, Try.promise() takes a Promise as an argument and returns an object with a method and a property: <result>.isError() and data or error. If the Promise resolves successfully, <result>.isError() will be false and data will contain the resolved value. If the Promise is rejected, <result>.isError() will be true and error will contain the error object.

License

This package is licensed under the MIT license. See the LICENSE file for more details.

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.2.1

1 year ago

1.0.0

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago