@casperengl/try-catch
Package to trycatch code that accepts functions and promises
Last updated 3 months 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.6.1                                ...           latest (3 months ago)

26 Versions

  • 1.6.1                                ...           3 months ago
  • 1.6.0                                ...           3 months ago
  • 1.5.3                                ...           3 months ago
  • 1.5.2                                ...           3 months ago
  • 1.5.1                                ...           3 months ago
  • 1.5.0                                ...           3 months ago
  • 1.4.5                                ...           4 months ago
  • 1.4.4                                ...           4 months ago
  • 1.4.3                                ...           4 months ago
  • 1.4.2                                ...           4 months ago
  • 1.4.1                                ...           6 months ago
  • 1.4.0                                ...           6 months ago
  • 1.3.1                                ...           6 months ago
  • 1.3.0                                ...           6 months ago
  • 1.2.0                                ...           6 months ago
  • 1.1.2                                ...           6 months ago
  • 1.1.1                                ...           6 months ago
  • 1.1.0                                ...           6 months ago
  • 1.0.7                                ...           6 months ago
  • 1.0.6                                ...           6 months ago
  • 1.0.5                                ...           6 months ago
  • 1.0.4                                ...           6 months ago
  • 1.0.3                                ...           6 months ago
  • 1.0.2                                ...           6 months ago
  • 1.0.1                                ...           6 months ago
  • 1.0.0                                ...           6 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 26
Last Day 0
Last Week 26
Last Month 0
Dependencies (0)
None
Dev Dependencies (14)
Dependents (1)

Copyright 2014 - 2017 © taobao.org |