ftry

1.0.16 • Public • Published

Flatry

Build Status GitHub license

Flatry (flat try) converting promise or function to flat array response with [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

Flatry example

 

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
let result = false;
try {
  result = mayThrowErrorSomeday();
} catch (error) {
  console.log("Error catched", error);
}
console.log("result", result);

// After
const [err, result] = flatry(mayThrowErrorSomeday);
if (err) console.log("Error catched", err);
console.log("result", result);

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.161latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.161
1.0.150

Package Sidebar

Install

npm i ftry

Weekly Downloads

1

Version

1.0.16

License

MIT

Unpacked Size

135 kB

Total Files

12

Last publish

Collaborators

  • leonsilicon