@bdsqqq/try

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

别让恐怖的捕捉塔毁了你那漂亮的一行人.

中文使用文档
文档目录4 节
  1. 使用量
  2. 这为什么存在?
  3. 为什么这真的存在?
  4. 捐款

尝试TM

别让恐怖的捕捉塔毁了你那漂亮的一行人.

使用量

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

这为什么存在?

Async 等待感觉像天堂,因为它避免了回叫地狱或"末日"(Pyramid of Doom),用行格式写出同声编码:

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

在错误处理开始之前... 因为那时你终于有了恐怖的Try-Catch塔 在那里你美丽的单行体神奇地扩张到至少5行代码:

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

   // ...
}

一个简单的解决方案是 附加 .catch() 每个承诺的结尾:


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

这种方法解决了问题,但可以有一点重复性,另一种方法是创建一个功能,执行一个尝试Catch来取代其他所有:


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

   // ...
}

为什么这真的存在?

我看着 一个火船短 最后在兔子洞里学习如何发布国家防范机制的套件. 这在您的代码库中仍然是个有趣的模式, 但最好粘贴副本而不是依赖.

我把源代码留在这儿 这样你就可以不用在 /src 文件夹中查找 .ts 文件了:

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

捐款

这个密码被公然盗取 一个火船youtube短,加上小的添加 data 从承诺中推断出这是个争论.

版本与发布记录

@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 是什么?

别让恐怖的捕捉塔毁了你那漂亮的一行人.

如何安装 @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 和提交记录判断。