v2.3

try™

bdsqqq/try
  • 0 dependencies
  • <1kb gzipped
  • Fully typesafe

Don't let the Try Catch Tower of Terror destroy your beautiful one liners.

Async-await feels like heaven because it avoids callback hell. But our responsibility to catch errors turned heaven into the try-catch tower of terror. Try™ gives you a taste of heaven without compromising functionality.

async function heaven() {
  const a = await step1();
  const b = await step2(a);
  const c = await step3(b);
  // ...
}
function hell() {
  step1((a) => {
    step2((b) => {
      step3((c) => {
        // ...
      });
    });
  });
}
async function Terror() {
  let a;
  let b;
  let c;

  try {
    a = await step1();
  } catch (error) {
    handle(error);
  }

  try {
    b = await step2(a);
  } catch (error) {
    handle(error);
  }

  try {
    c = await step3(b);
  } catch (error) {
    handle(error);
  }

  // ...
}
import { trytm } from "@bdsqqq/try"

async function awesome() {
   const [aData, aError] = await trytm(step1());
   if(aError) // ...

   const [bData, bError] = await trytm(step2(aData));
   if(bError) // ...

   const [cData, cError] = await trytm(step3(bData));
   if(cError) // ...

   // ...
}