rentzsch / catchr

es6 const assignment even with exceptional code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Instead of:

let x;
try {
  x = exceptionThrowingFunction();
} catch(ex) {
  x = reasonableDefault;
}
useX(x);

This:

const [x] = catchr(_ => exceptionThrowingFunction(), reasonableDefault);
useX(x);

You can also receive the exception:

const [x, err] = catchr(_ => exceptionThrowingFunction(), reasonableDefault);
if (err.name !== "SyntaxError") {
  useX(x);
}

About

es6 const assignment even with exceptional code


Languages

Language:JavaScript 100.0%