@benzinga/safe-await

中文文档
1.0.6发布于 2025/04/11MITTypeScriptES Module零依赖近 30 天下载 2879

This Project is Made Possible thanks to

原始使用文档
文档目录8 节
  1. SafePromise
  2. safeAwait
  3. safeAwaitFunction
  4. Helper Functions
  5. safeRace
  6. safeMap
  7. safeThen
  8. safeAwaitAll

safe-await

This Project is Made Possible thanks to

Benzinga Logo

ZSoft Logo

Safe Await is a simple way to make sure that our errors are handled.

SafePromise

One of the things I don't like about JavaScript async/await syntax is that it throws on error. This has always caused issues for me since people always forget to try catch these await statements. The other issue I have is the new scope that is created when you try catch which simply makes the code look awful because you have to start thinking about scope of variables.

safeAwait

We don't have to create a safe function we can simply transform the function call to a safe call such as:

const stuff = await safeAwait(fetch('benzinga.com/things/and/stuff'));
if (stuff.err) {
  console.log(stuff.err);
}
else {
  console.log(stuff.ok);
}

As you can see all we are doing here is wrapping fetch with the safeAwait function which does the following:

export type SafePromise<T> = Promise<{ err: Error; ok: undefined } | { err: undefined; ok: T}>

export const safeAwait = async <T>(promise: Promise<T>): SafePromise<T> => {
  try {
    return { ok: await promise };
  } catch (err: any) {
    return { err };
  }
};

One thing to note about both is that safeAwait returns a SafePromise. This is used to indicate that you do not have to try catch the Promise and its an indication that this was checked somewhere upstream. Because the only time you really need to try catch is when calling API functions. After that point Promises are safe unless you are throwing exceptions yourself.

safeAwaitFunction

Another way to solve this issue is to wrap the function to make a safeFunction. So what does safeFunction do? well its very simply it takes a function as an argument and returns a function. Internally it is simply doing the try catch for you and tupalizing the result.

const stuff = await safeFetch('benzinga.com/things/and/stuff');
if (stuff.err) {
  console.log(stuff.err);
} else {
  console.log(stuff.ok);
}

Creating a safe function is easy. Simply call safeAwaitFunction with an async function as a parameter.

const safeFetch = safeAwaitFunction(fetch);

Now we have an implantation of fetch that always returns a SafePromise.

This is the core concept of the safe-await package. The following are simply helper functions and tools that simply use the safe-await library.

Helper Functions

safeRace

Similar to Promise.Race but for safePromises.

safeRace = <T>(promises: SafePromise<T>[]): SafePromise<T>

safeMap

A function that is similar to 'map' for arrays but for safe awaits

example

const quotesExist = (symbol) => {
  safeMap(fetchQuote(symbol), quote => quote.doesExist)
};

safeThen

A function that is similar to Promise.then which allows you to add a function to run after the completion of the promise.

example

const quotesExist = (symbol) => {
  safeThen(fetchQuote(symbol), quote => { ok: quote.doesExist })
};

safeAwaitAll

Similar to Promise.all however it returns a SafePromiseMultiError which allows:

safeAwaitAll = async <U, T extends readonly SafePromise<U>[]>(promise: T & SafePromise<U>[]): SafePromiseMultiError<U[]>

版本与发布记录

@benzinga/safe-await 共发布 8 个版本,首次发布于 2023/02/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.6latest2025/04/111.6 年前
1.0.52025/04/111.6 年前
1.0.42025/04/111.6 年前
1.0.32025/04/111.6 年前
1.0.22024/11/162 年前
1.0.12024/10/282 年前

关于 @benzinga/safe-await 的常见问题

@benzinga/safe-await 是什么?

This Project is Made Possible thanks to

如何安装 @benzinga/safe-await?

在项目目录运行 npm install @benzinga/safe-await,或使用 pnpm add / yarn add / bun add。

@benzinga/safe-await 支持 TypeScript 吗?

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

@benzinga/safe-await 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@benzinga/safe-await 可以商用吗?

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

@benzinga/safe-await 的下载量有多少?

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

@benzinga/safe-await 还在维护吗?

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