@expo/results

中文文档
1.0.0发布于 2020/04/30MITTypeScriptCommonJS零依赖近 30 天下载 718.2万

An efficient, standards-compliant library for representing results of successful or failed operations

原始使用文档

@expo/results

Tests codecov

An efficient, standards-compliant library for representing results of successful or failed operations. A result object represents the result of an operation that can either return a value successfully or fail. Typically we'd simply either return a value or throw an error, but sometimes we perform multiple operations as a batch, some of which may succeed and others fail. Since we can't simultaneously return values and throw errors, we instead return collections of result objects. This allows a batch operation to return values for successful operations and errors for failed ones without loss of information, namely the errors. (In contrast, sometimes it is appropriate for a batch operation to return just successful values and omit values for failed operations.)

Usage

Using Results

import { Result, result } from '@expo/results';

const results = await fetchWebPages(['https://expo.dev', 'http://example.com']);
for (const result of results) {
  if (result.ok) {
    console.log(result.value);
  } else {
    console.error(result.reason);
  }
}

Creating Results

import { Result, result } from '@expo/results';

/**
 * The purpose of this result API is to let you write functions that can
 * partially succeed and partially fail and return all of that information to
 * the caller.
 */
function fetchWebPages(urls: string[]): Promise<Result<string>[]> {
  return Promise.all(urls.map(fetchWebPage));
}

function fetchWebPage(url: string): Promise<Result<string>> {
  try {
    const response = await fetch(url);
    const text = await response.text();
    return result(text);
  } catch (e) {
    return result(e);
  }
}

版本与发布记录

@expo/results 共发布 5 个版本,首次发布于 2019/12/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2020/04/306 年前
0.3.02020/01/246 年前
0.2.02020/01/086 年前
0.1.12019/12/316 年前
0.1.02019/12/216 年前

关于 @expo/results 的常见问题

@expo/results 是什么?

An efficient, standards-compliant library for representing results of successful or failed operations

如何安装 @expo/results?

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

@expo/results 支持 TypeScript 吗?

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

@expo/results 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@expo/results 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@expo/results 可以商用吗?

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

@expo/results 的下载量有多少?

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

@expo/results 还在维护吗?

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