![]() | Name | Last modified | Size | Description |
---|---|---|---|---|
![]() | Parent Directory | - | ||
![]() | legacy/ | 2 years ago | - | |
![]() | lib/ | 2 years ago | - | |
![]() | ChangeLog | 2 years ago | 367 | 0fb859dc fixed mobile overflwo options [كارل مبارك] |
![]() | LICENSE | 2 years ago | 1.0K | 0fb859dc fixed mobile overflwo options [كارل مبارك] |
![]() | README.md | 2 years ago | 2.3K | 0fb859dc fixed mobile overflwo options [كارل مبارك] |
![]() | package.json | 2 years ago | 2.1K | 0fb859dc fixed mobile overflwo options [كارل مبارك] |
Functional try-catch
wrapper for promises
.
npm i try-to-catch
Wrap function to avoid try-catch
block, resolves [error, result]
;
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);
}
In old node.js
environments that not fully supports es2015
, try-to-catch
can be used with:
var tryToCatch = require('try-to-catch/legacy');
MIT