Skip to content
 
 

Latest commit

 

History

27 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flatry

Build Status codecov GitHub license

Flatry (flat try) converting promise or function to flat array response like [err, result].

Inspired by golang style error handling without try/catch.

 

Install

npm install flatry
# or
yarn add flatry

 

Use

import flatry from 'flatry';
// or
const flatry = require('flatry');

 

Examples

Asynchronous (async/await)

Before:

async asyncData({ app, error }) {
  try {
    const categories = (await app.$axios.$get('forum')).sections;
    return { categories };
  } catch (err) {
    return error({ statusCode: err.statusCode });
  }
}

After:

async asyncData({ app, error }) {
  const [err, res] = await flatry(app.$axios.$get('forum'));
  if (err) return error({ statusCode: err.statusCode });
  return { categories: res.sections };
}

Synchronous

Before:

function throwErrorIn10percent() {
  if (Math.random() > 0.9) throw new Error('Random error');
  return true;
}

let result = false;
try {
    result = throwErrorIn10percent()
} catch (error) {
    console.log('Error catched', error)
}
console.log('result', result);

After:

function throwErrorIn10percent() {
  if (Math.random() > 0.9) throw new Error('Random error');
  return true;
}

const [err, result] = flatry(throwErrorIn10percent);
if (err) console.log('Error catched', err)
console.log('result', result);

About

Flatry — try/catch free error handling module for JavaScript

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages