@bdsqqq/try

中文文档
2.3.1发布于 2023/02/27MITTypeScriptESM + CJS零依赖近 30 天下载 2146

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

原始使用文档
文档目录4 节
  1. Usage
  2. Why does this exist?
  3. Why does this REALLY exist?
  4. Attributions

Try™

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

Usage

npm install @bdsqqq/try

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

const mockPromise = () => {
  return new Promise<string>((resolve, _) => {
    setTimeout(() => {
      resolve("hello from promise");
    }, 1000);
  });
};

const mockPromiseThatFails = () => {
  return new Promise<string>((_, reject) => {
    setTimeout(() => {
      reject(new Error("hello from promise"));
    }, 1000);
  });
};

const [data, error] = await trytm(mockPromise());
const [data2, error2] = await trytm(mockPromiseThatFails());

Why does this exist?

Async await feels like heaven because it avoids the callback hell or Pyramid of Doom by writing asyncronous code in a line by line format:

function hell() {
   step1((a) => {
      step2((b) => {
         step3((c) => {
            // ...
         })
      })
   })
}

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

Until error handling comes into play... Because then you end up with the Try-Catch Tower of Terror, where your beautiful one-liners magically expand to at least 5 lines of code:

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

   // ...
}

An easy solution would be to append the .catch() method to the end of each promise:


async function easy(){
   const a = await step1().catch(err => handle(err));
   const b = await step2(a).catch(err => handle(err));
   const c = await step3(b).catch(err => handle(err));
   // ...
}

This approach solves the issue but can get a bit repetitive, another approach is to create a function that implements one Try Catch to replace all the others:


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) // ...

   // ...
}

Why does this REALLY exist?

I watched a fireship short and ended up in a rabbit hole to learn how to publish a NPM package. This is still an interesting pattern to use in your codebase but might be best copy pasted instead of being a dependency.

I'll leave the source code here so you don't have to look for the one .ts file in the /src folder:

export const trytm = async <T>(
   promise: Promise<T>,
): Promise<[T, null] | [null, Error]> => {
   try {
      const data = await promise;
      return [data, null];
   } catch (throwable) {
      if (throwable instanceof Error) return [null, throwable];

      throw throwable;
   }
};

Attributions

This code is blatantly stolen from a fireship youtube short, with minor additions to make data infer its typing from the promise passed as an argument.

版本与发布记录

@bdsqqq/try 共发布 9 个版本,首次发布于 2023/02/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.3.1latest2023/02/273 年前
2.3.02023/02/263 年前
2.2.02023/02/253 年前
2.1.22023/02/243 年前
2.1.12023/02/243 年前
2.1.02023/02/233 年前

关于 @bdsqqq/try 的常见问题

@bdsqqq/try 是什么?

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

如何安装 @bdsqqq/try?

在项目目录运行 npm install @bdsqqq/try,或使用 pnpm add / yarn add / bun add。

@bdsqqq/try 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bdsqqq/try 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bdsqqq/try 可以商用吗?

@bdsqqq/try 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bdsqqq/try 的下载量有多少?

根据 npm 官方下载统计,@bdsqqq/try 在 2026/10/08 – 2026/11/06 这 30 天内被下载 2,146 次,日均约 72 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bdsqqq/try 还在维护吗?

最新版本 2.3.1 发布于 2023/02/27(3 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/02/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。