# @attio/fetchable 中文文档

规范地址：https://npmcn.com/package/%40attio/fetchable/

版本：0\.0\.1-experimental\.9
npm 元数据更新时间：2025-12-04T11:06:37.652Z
中文译文更新时间：2026-11-07T04:06:39.631Z
软件包许可：未声明

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40attio/fetchable
原文对照：https://npmcn.com/package/%40attio/fetchable/?lang=original

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：49.8 kB，6 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2025-03-13T11:36:00.317Z
- 最新版本发布：2025-12-04T11:06:37.403Z
- 近 30 天下载量：57183（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：8（近 12 个月 3 个）

## 中文简介

`fetchable` 是一个用于在 Typescript 和 JavaScript 中明确处理错误和加载的库.

## 安装

```sh
npm install @attio/fetchable
```

也可以使用：`pnpm add @attio/fetchable`、`yarn add @attio/fetchable`、`bun add @attio/fetchable`

## README

# 可获取

`fetchable` 是一个用于在 Typescript 和 JavaScript 中明确处理错误和加载的库.

## 安装

```sh
> npm install @attio/fetchable
```

## 概念

### 初级国家

有三个原始的状态 `fetchable`。 。 。 .

#### ++ 完成( E)

A 类 `Complete<V>` 是一个简单的 JavaScript 对象,它代表任何种类的完整数据.

```typescript
import {complete} from "@attio/fetchable"

const completeNumber = complete(1) // Returns a Complete<1>

console.log(completeNumber.value) // 1
console.log(completeNumber.state) // "complete"
```

#### QQ 错误

一个 `Errored<E>` 是另一个代表出错的 JavaScript 对象 .

```typescript
import {errored} from "@attio/fetchable"

const erroredThing = errored("Oops") // Returns an Errored<"Oops">

console.log(erroredThing.error) // "Oops"
console.log(erroredThing.state) // "errored"
```

#### 待定

A 类 `Pending` 是另一个 JavaScript 对象,它代表着一个进程正在进行中.

```typescript
import {pending} from "@attio/fetchable"

const pendingThing = pending() // Returns a Pending

console.log(pendingThing.state) // "pending"
```

### 上级州

当我们开始把三个原始国家混为一谈时,乐趣就来了.

#### 结果: 完成或出错

A 类 `Result<V, E>` 对象是 `Complete<V>` 或一个 `Errored<E>`它代表着 可能成功或失败的进程的结果.

```typescript
import {isComplete} from "@attio/fetchable"

const result = doSynchronousThingWhichMightError() // Returns Result<number, "Oops">

if (isComplete(result)) {
    console.log(result.value) // number
} else {
    console.log(result.error) // "Oops"
}
```

#### 可装入: 完成或待发

A 类 `Loadable<V>` 对象是 `Complete<V>` 或一个 `Pending`它代表着 同步进程的结果不能失败.

```typescript
import {isComplete} from "@attio/fetchable"

const loadable = getStatusOfLongRunningProcess() // Returns Loadable<number>

if (isComplete(loadable)) {
    console.log(loadable.value) // number
}
```

#### 可获取: 完成或待发或出错

A 类 `Fetchable<V, E>` 对象是 `Complete<V>`, (中文(简体) ), `Errored<E>` 或者说 `Pending`。 代表一个可能失败的同步进程的结果.

`Fetchable` 最常见于声明前端代码 我们不想暴露承诺.

```typescript
import {isComplete, isErrored} from "@attio/fetchable"

const fetchable = getStatusOfLongRunningProcessWhichMightFail() // Returns Fetchable<number, "Oops">

if (isComplete(fetchable)) {
    console.log(fetchable.value) // number
} else if (isErrored(fetchable)) {
    console.log(fetchable.error) // "Oops"
}
```

#### 同步结果

一个 `AsyncResult<V, E>` 是一个承诺 `Result<V, E>`。 。 。 . `Complete<V>`, (中文(简体) ), `Errored<E>` 或者说 `Pending`。 。 。 . 它常常出现在我们利用承诺进行同步进程的强制性守则中.

```typescript
import {isComplete, isErrored} from "@attio/fetchable"

const asyncResult = runAsyncProcessWhichMightFail() // Returns AsyncResult<number, "Oops">

asyncResult.then(result => {
    if (isComplete(result)) {
        console.log(result.value) // number
    } else if (isErrored(result)) {
        console.log(result.error) // "Oops"
    }
})
```

### 转换

`fetchable` 为与这些物体合作提供功能性设施,无须明确检查 他们处于什么状态.

#### 地图

```typescript
import {map} from "@attio/fetchable"

const fetchable = getStatusOfLongRunningProcessWhichMightFail() // Returns Fetchable<number, "Oops">

// Increments the fetchable if it's complete. 
// Returns Fetchable<number, "Oops">
const incrementedFetchable = map(fetchable, (number) => number + 1)
```

#### 绑定

```typescript
import {map} from "@attio/fetchable"

const fetchable = getStatusOfLongRunningProcessWhichMightFail() // Returns Fetchable<number, "Oops">

// Increments the fetchable if it's complete and not zero.
// Returns Fetchable<number, "Oops" | "Unexpected zero">
const incrementedFetchable = bind(fetchable, (number) =>
    number === 0 ? errored("Unexpected zero") : complete(number + 1)
)
```

#### 组合

阵列

```typescript
import {combine} from "@attio/fetchable"

const fetchable1 = getStatusOfLongRunningProcessWhichMightFail() // Returns Fetchable<number, "Oops">
const fetchable2 = doSynchronousThingWhichMightError() // Returns Result<string, "Eek">

// If both fetchables are complete then their values will be returned in a complete array.
// Returns Fetchable<[number, string], "Oops" | "Eek">
const combinedFetchable = combine([fetchable1, fetchable2])
```

对象

```typescript
import {combine} from "@attio/fetchable"

const fetchableA = getStatusOfLongRunningProcessWhichMightFail() // Returns Fetchable<number, "Oops">
const fetchableB = doSynchronousThingWhichMightError() // Returns Result<string, "Eek">

// If both fetchables are complete then their values will be returned in a new object.
// Returns Fetchable<{a: number, b: string}, "Oops" | "Eek">
const combinedFetchable = combine({a: fetchable1, b: fetchable2})
```

#### 组合 Async

喜欢 `Promise.all` 页:1 `AsyncResult`编号 需要多个 `AsyncResult`当他们都决定 解决 `Complete`或当一个人与 `Errored`。 。 。 .

阵列

```typescript
import {combineAsync} from "@attio/fetchable"

const asyncResult1 = runAsyncProcessWhichMightFail() // Returns AsyncResult<number, "Oops">
const asyncResult2 = runAnotherAsyncProcessWhichMightFail() // Returns AsyncResult<string, "Eek">

// Returns Result<[number, string], "Oops" | "Eek">
const combinedResult = await combineAsync([asyncResult1, asyncResult2])
```

对象

```typescript
import {combine} from "@attio/fetchable"

const asyncResultA = runAsyncProcessWhichMightFail() // Returns AsyncResult<number, "Oops">
const asyncResultB = runAnotherAsyncProcessWhichMightFail() // Returns AsyncResult<string, "Eek">

// If both fetchables are complete then their values will be returned in a new object.
// Returns Result<{a: number, b: string}, "Oops" | "Eek">
const combinedResult = await combineAsync({a: asyncResultA, b: asyncResultB})
```

## 常见问题

### @attio/fetchable 是什么？

fetchable 是一个用于在 Typescript 和 JavaScript 中明确处理错误和加载的库\.

### 如何安装 @attio/fetchable？

在项目目录运行 npm install @attio/fetchable，或使用 pnpm add / yarn add / bun add。

### @attio/fetchable 支持 TypeScript 吗？

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

### @attio/fetchable 支持 ESM 还是 CommonJS？

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

### @attio/fetchable 可以商用吗？

@attio/fetchable 使用 未声明 许可。package\.json 没有声明许可证。没有许可证的代码默认保留全部权利，商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括，不构成法律意见，请以许可原文为准。

### @attio/fetchable 的下载量有多少？

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

### @attio/fetchable 还在维护吗？

最新版本 0\.0\.1-experimental\.9 发布于 2025/12/04（11 个月前）；至今共发布 8 个版本，其中过去 12 个月发布了 3 个；首次发布于 2025/03/13。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

