You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Andy Stewart ca52b4b383 Add mermaid plugin. 6 years ago
..
legacy Add mermaid plugin. 6 years ago
lib Add mermaid plugin. 6 years ago
ChangeLog Add mermaid plugin. 6 years ago
LICENSE Add mermaid plugin. 6 years ago
README.md Add mermaid plugin. 6 years ago
package.json Add mermaid plugin. 6 years ago

README.md

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');
await tryToCatch(Promise.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 fs = require('fs');
const tryToCatch = require('try-to-catch');
const {promisify} = require('util');
const readFile = promisify(fs.readFile);
const readDir = promisify(fs.readdir);

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);
}

Environments

In old node.js environments that not fully supports es2015, try-to-catch can be used with:

var tryToCatch = require('try-to-catch/legacy');

License

MIT