0.1.5 • Published 4 years ago

with-error v0.1.5

Weekly downloads
2
License
ISC
Repository
github
Last release
4 years ago

with-error

Either monad for work with exceptions in JavaScript. Go-style.

NPM version Build Status Dependency Status Coverage percentage

Why?

Because, exceptions may be the way to callback-hell.

try{
    func();
}catch(e){
    try{
        func2()
    }catch(e){
        // HELL
    }    
}

Install

npm install with-error --save

or

yarn add with-error

Usage

import withError from "with-error";

// Non-promisify successfully result
const { result } = withError(() => "result1");

console.log(result.toUpperCase()); // RESULT1

// Non-promisify failure result
const { error, result } = withError((): string => { throw new Error("Error1"); } );

if (error) {
    console.log(error.toString()); // Error1
}

// Promisify successfully result
const { result } = await withError(() => Promise.resolve("result1"));

console.log(result.toUpperCase()); // RESULT1

// Non-promisify failure result

const { result, error } = await withError(() => Promise.reject(new Error("Error1")));
if (error) {
        console.log(error.toString()); // Error1
}

// Also supported array-like response

const [users, error] = await withError(() => Promise.resolve(["user1"]));

API

// Response
type IWithErrorReturn<R> = [
    R,
    any
] & { error: any, result: R };
// non-promisify with-error
interface IWithError {
    <R>(cb: () => R): IWithErrorReturn<R>;
}
// promisify with-error
interface IWithError {
    <R>(cb: () => Promise<R>): Promise<IWithErrorReturn<R>>;
}

Test

npm install
npm test
0.1.4

4 years ago

0.1.5

4 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago