0.1.0 • Published 2 years ago

@meetsnowmaker/trycatcher v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@meetsnowmaker/trycatcher

npm (scoped) npm bundle size (scoped)

Embrace failure in synchronous functions.

Hello 0.1.x

The default export is set in stone but further flavors are coming.

Introduction

Ever since I started learning about buzzwords like functional programming and asynchronous functions in node, the old way of trying and catching errors felt off. To avoid making every function asynchronous just to be able to properly return the desired outcome to the const I defined, I made a simple hacky attempt to get a predetermined response from the tryable function.

As I naturally progressed to urge for maximum immutability, it felt somewhat more convenient to define both outcomes with a single call. After a while I noticed this simple function had numerous copies in almost every project I worked on. I was thinking it might be a great opportunity to learn more about the npm packaging lifecycle, so here it is. My first dummy npm project. Bye let you might be missed.

Please note: this is definitely not an original idea, just my take on a solution.

How to

The default version returns with an array where index 0 is always either null or the desired outcome, and index 1 is null or the caught error. When defining you can destruct the array and get properly named variables for the predetermined indexes. I prefer using arrow functions for the argument. You can express more complex, like multi-line or multi-step operations inside the curly braces, just dont forget to use a return on the end. See examples for more use cases. I might add useful ones eventually.

import tryCatcher from '@meetsnowmaker/trycatcher';

const shouldWork = JSON.stringify(['asd', 'kek']);
const shouldFail = '["asd",kek]';

const [parsed, error] = tryCatcher(() => JSON.parse(shouldWork));

if (error) {
  throw error;
}

console.log('parsed', parsed);

const [maybeParsed, definitelyError] = tryCatcher(() => JSON.parse(shouldFail));

if (definitelyError) {
  throw definitelyError;
}

console.log('maybeParsed', maybeParsed);

will yield

parsed [ 'asd', 'kek' ]
C:\...\example.js:42
  throw definitelyError;
  ^

SyntaxError: Unexpected token k in JSON at position 7
    at JSON.parse (<anonymous>)

Roadmap

Eventually I should implement proper type support and more variables of the base function.

Things I want to add in the future:

  • typescript
  • object like return with undefined instead of null, so one can inject default value when destructuring
  • webpack for minifying
  • some real life examples
  • some real life test scenarios with BDD
  • GitHub Actions for publishing
0.1.0

2 years ago

0.1.0-alpha.4

2 years ago

0.1.0-alpha.3

2 years ago

0.1.0-alpha.2

2 years ago

0.1.0-alpha.1

2 years ago

0.0.1-alpha.8

2 years ago

0.0.1-alpha.7

2 years ago

0.0.1-alpha.6

2 years ago

0.0.1-alpha.5

2 years ago

0.0.1-alpha.3

2 years ago

0.0.1-alpha.2

2 years ago

0.0.1-alpha.1

2 years ago