async-wrapus
v1.0.4
Published
Wrapper for async functions without pain. No try catches anymore.
Downloads
10
Maintainers
Readme
async-wrapus
Wrapper for async functions without pain. No try catches anymore.
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
}
};