@casperengl/try-catch
Package to trycatch code that accepts functions and promises
Last updated a day ago by casperengl .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @casperengl/try-catch 
SYNC missed versions from official npm registry.

Welcome to @casperengl/try-catch ????

Version GitHub Action Documentation Maintenance License: MIT Twitter: casperengl

Package to trycatch code that accepts functions and promises

???? Homepage

Install

npm install @casperengl/try-catch

# or
yarn add @casperengl/try-catch

Usage

ES2015+

import { tryCatch } from '@casperengl/try-catch';

// Or default import
import tryCatch from '@casperengl/try-catch';

CommonJS

const { tryCatch } = require('@casperengl/try-catch');

Function

(async () => { // success
  const fn = () => 'success';
  
  const [error, result] = await tryCatch(fn);

  console.log([error, result]); // [null, 'success']
})();

(async () => { // error
  const fn = () => {
    if (true) {
      throw new Error('An error occurred');
    }

    return 'success';
  };
  
  const [error, result] = await tryCatch(fn);

  console.log([error, result]); // [[Error: An error occurred], undefined]
})();

Promise

(async () => { // success
  const promise = () => Promise.resolve('success');

  const [error, result] = await tryCatch(promise);

  console.log([error, result]) // [null, 'success']
})();

(async () => { // error
  const promise = () => Promise.reject('An error occurred');

  const [error, result] = await tryCatch(promise);

  console.log([error, result]) // [[Error: An error occurred], undefined]
})();

Axios

Or the likes of Axios

(async () => {
  // Do not unwrap the response with `await`
  const promise = axios.get('https://reqres.in/api/users/1');

  const [error, result] = await tryCatch(promise);

  console.log([error, result]) // [null, { ..., data: { data: { first_name: 'George', last_name: 'Bluth' } } }]
})();

Run tests

npm run test

Author

???? Casper Engelmann me@casperengelmann.com

???? Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

???? License

Copyright © 2019 Casper Engelmann me@casperengelmann.com.
This project is MIT licensed.


This README was generated with ❤️ by readme-md-generator

Current Tags

  • 1.4.5                                ...           latest (a day ago)

20 Versions

  • 1.4.5                                ...           a day ago
  • 1.4.4                                ...           13 days ago
  • 1.4.3                                ...           13 days ago
  • 1.4.2                                ...           13 days ago
  • 1.4.1                                ...           2 months ago
  • 1.4.0                                ...           2 months ago
  • 1.3.1                                ...           2 months ago
  • 1.3.0                                ...           2 months ago
  • 1.2.0                                ...           2 months ago
  • 1.1.2                                ...           2 months ago
  • 1.1.1                                ...           2 months ago
  • 1.1.0                                ...           2 months ago
  • 1.0.7                                ...           3 months ago
  • 1.0.6                                ...           3 months ago
  • 1.0.5                                ...           3 months ago
  • 1.0.4                                ...           3 months ago
  • 1.0.3                                ...           3 months ago
  • 1.0.2                                ...           3 months ago
  • 1.0.1                                ...           3 months ago
  • 1.0.0                                ...           3 months ago
Maintainers (1)
Downloads
Today 1
This Week 20
This Month 73
Last Day 19
Last Week 0
Last Month 37
Dependencies (0)
None
Dev Dependencies (15)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |