try-to-catch
Version:
function try-catch wrapper for promises
75 lines (51 loc) • 1.78 kB
Markdown
//img.shields.io/npm/v/try-to-catch.svg?style=flat&longCache=true
[ ]: https://img.shields.io/travis/coderaiser/try-to-catch/master.svg?style=flat&longCache=true
[ ]: https://npmjs.org/package/try-to-catch "npm"
[ ]: https://travis-ci.org/coderaiser/try-to-catch "Build Status"
[ ]: https://coveralls.io/github/coderaiser/try-to-catch?branch=master
[ ]: https://coveralls.io/repos/coderaiser/try-to-catch/badge.svg?branch=master&service=github
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`:
```js
const tryToCatch = require('try-to-catch');
const reject = Promise.reject.bind(Promise);
await tryToCatch(reject, 'hi');
// returns
// [ Error: hi]
```
Can be used with functions:
```js
const tryToCatch = require('try-to-catch');
await tryToCatch(() => 5);
// returns
[ ];
```
Advanced example:
```js
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);
}
```
- [try-catch](https://github.com/coderaiser/try-catch "try-catch") - functional try-catch wrapper.
MIT
[ ]: https: