1.9.0 ā€¢ Published 3 years ago

@casperengl/try-catch v1.9.0

Weekly downloads
310
License
MIT
Repository
github
Last release
3 years ago

Package to trycatch code that accepts functions and promises

šŸ  Homepage

šŸ”Ž Example (Inspect page for source)

Install

npm install @casperengl/try-catch

# or
yarn add @casperengl/try-catch

Usage

Bundler (Webpack, Rollup, etc.)

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

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

Browser (ESM)

Notice type="module" is required to use import statements in the browser.

<script type="module">
  import { tryCatch } from 'https://cdn.skypack.dev/@casperengl/try-catch';
  // Or default import
  // import tryCatch from 'https://cdn.skypack.dev/@casperengl/try-catch';

  const apiCall = async () => {
    const promise = fetch('https://reqres.in/api/users/1').then((response) => response.json());
    const [error, result] = await tryCatch(promise);

    console.log(error, result); // null, {data: {ā€¦}, support: {ā€¦}}
  };

  apiCall();
</script>

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

1.10.0-2

3 years ago

1.10.0-1

3 years ago

1.10.0-0

3 years ago

1.9.0

3 years ago

1.9.0-1

3 years ago

1.9.0-0

3 years ago

1.8.3

3 years ago

1.8.2

3 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.7.2

3 years ago

1.7.1

3 years ago

1.7.0

4 years ago

1.6.4

4 years ago

1.6.3

4 years ago

1.6.2

4 years ago

1.6.6

4 years ago

1.6.5

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.5

4 years ago

1.4.4

4 years ago

1.4.3

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.1

4 years ago

1.2.0

4 years ago

1.3.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.1.2

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago