try-to-catch

WebJar for try-to-catch

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

try-to-catch
Last Version

Last Version

1.1.1
Release Date

Release Date

Type

Type

jar
Description

Description

try-to-catch
WebJar for try-to-catch
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/coderaiser/try-to-catch

Download try-to-catch

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/try-to-catch/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>try-to-catch</artifactId>
    <version>1.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/try-to-catch/
implementation 'org.webjars.npm:try-to-catch:1.1.1'
// https://jarcasting.com/artifacts/org.webjars.npm/try-to-catch/
implementation ("org.webjars.npm:try-to-catch:1.1.1")
'org.webjars.npm:try-to-catch:jar:1.1.1'
<dependency org="org.webjars.npm" name="try-to-catch" rev="1.1.1">
  <artifact name="try-to-catch" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='try-to-catch', version='1.1.1')
)
libraryDependencies += "org.webjars.npm" % "try-to-catch" % "1.1.1"
[org.webjars.npm/try-to-catch "1.1.1"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

Try to Catch NPM version Dependency Status Build Status Coverage Status

Functional try-catch wrapper for promises.

Install

npm i try-to-catch

API

tryToCatch(fn, [...args])

Wrap function to avoid try-catch block, resolves [error, result];

Example

Simplest example with async-await:

const tryToCatch = require('try-to-catch');
const reject = Promise.reject.bind(Promise);
await tryToCatch(reject, 'hi');
// returns
[ Error: hi]

Can be used with functions:

const tryToCatch = require('try-to-catch');
await tryToCatch(() => 5);
// returns
[null, 5]

Advanced example:

const {readFile, readdir} = require('fs').promises;
const tryToCatch = require('try-to-catch');

read(process.argv[2])
    .then(console.log)
    .catch(console.error);

async function read(path) {
    const [error, data] = await tryToCatch(readFile, path, 'utf8');
    
    if (!error)
        return data;
    
    if (error.code !== 'EISDIR')
        return error;
    
    return await readdir(path);
}

Related

License

MIT

Versions

Version
1.1.1