Package Exports
- try-to-catch
- try-to-catch/lib/try-to-catch.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (try-to-catch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Try to Catch

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
- try-catch - functional try-catch wrapper.
License
MIT