Package Exports
- flatry
- flatry/cjs/index.js
- flatry/esm/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (flatry) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Flatry
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

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