可获取
fetchable 是一个用于在 Typescript 和 JavaScript 中明确处理错误和加载的库.
安装
> npm install @attio/fetchable
概念
初级国家
有三个原始的状态 fetchable。 。 。 .
++ 完成( E)
A 类 Complete<V> 是一个简单的 JavaScript 对象,它代表任何种类的完整数据.
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 对象 .
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 对象,它代表着一个进程正在进行中.
import {pending} from "@attio/fetchable"
const pendingThing = pending() // Returns a Pending
console.log(pendingThing.state) // "pending"
上级州
当我们开始把三个原始国家混为一谈时,乐趣就来了.
结果: 完成或出错
A 类 Result<V, E> 对象是 Complete<V> 或一个 Errored<E>它代表着
可能成功或失败的进程的结果.
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它代表着
同步进程的结果不能失败.
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 最常见于声明前端代码 我们不想暴露承诺.
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。 。 。 .
它常常出现在我们利用承诺进行同步进程的强制性守则中.
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 为与这些物体合作提供功能性设施,无须明确检查
他们处于什么状态.
地图
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)
绑定
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)
)
组合
阵列
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])
对象
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。 。 。 .
阵列
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])
对象
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})