async-wrapus

Wrapper for async functions without pain. No try catches anymore.

Usage no npm install needed!

<script type="module">
  import asyncWrapus from 'https://cdn.skypack.dev/async-wrapus';
</script>

README

async-wrapus

Wrapper for async functions without pain. No try catches anymore.

NPM JavaScript Style Guide Badges Badges Badges Badges Badges Badges

Description

Wrapper returns array of Error object and Result. In case function throws no exception it will return [null, resultObject]. In case function throws with exception it will return [Error, null].

Install

npm install --save async-wrapus
yarn add  async-wrapus

Usage

import asyncWrap from 'async-wrapus';

const asyncRequest = async () => {
  // Can throw exception!
  return await someApiRequest();
};

const someMethod = async () => {
  /**
   * err: null | Error
   * result: Result of asyncRequest | null
   */
  const [err, result] = await asyncWrap(asyncRequest());

  if (err) {
    // do something with exception
  }

  if (result) {
    // do something with result
  }
};