kolengri / async-wrapus

🍭 Make async requests sweet again! Wrapper for async functions without pain. No try catches anymore.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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
  }
};

About

🍭 Make async requests sweet again! Wrapper for async functions without pain. No try catches anymore.

License:MIT License


Languages

Language:TypeScript 100.0%