GPTScript / AiScript

A Minimal, Full-Stack, Tool-Assisted Language. Native to Browsers and Bun. Strictly & Strongly-Typed.
https://github.com/GPTScript/AiScript
Mozilla Public License 2.0
9 stars 1 forks source link

NO async try/catch #3

Open coolaj86 opened 2 years ago

coolaj86 commented 2 years ago

Sync

try/catch is for parsers where you don't want to catch every possible little error, but the error can be handled in a particular way.

It should be no more than 6-10 lines long:

let data;
try {
  data = JSON.parse(json)
} catch(e) {
  data = {};
}

DO NOT execute many instructions in a try/catch block. Encapsulate things that need to be tryd in a function.

Async

Don't use try/catch. Async functions are promises. Use .catch().

await doStuff().catch(function (e) {
  throw new Error("oversimplified explanation", { cause: e });
});
let data = await doStuff().catch(function (e) {
  if ('NOT_FOUND' !== e.code) {
    throw e;
  }
  return {};
});