fp-taskeither-ref – skill Lập trình cho agent AI
Tài liệu tham khảo nhanh về TaskEither. Dùng khi cần xử lý lỗi bất đồng bộ, gọi API hoặc thao tác dựa trên Promise có thể thất bại.
Cài skill fp-taskeither-ref bằng một lệnh
Chọn công cụ bạn đang dùng, copy lệnh và dán vào terminal. Skill sẽ nằm trong thư mục skills của công cụ đó và sẵn sàng dùng ở phiên làm việc tiếp theo.
Claude Code
mkdir -p ~/.claude/skills && curl -fsSL https://skill.dn1nguoi.com/z/fp-taskeither-ref-1b8d3.tgz | tar xz -C ~/.claude/skills
Gọi trong Claude Code: /fp-taskeither-ref – rồi mô tả công việc bạn muốn làm.
Codex
mkdir -p ~/.codex/skills && curl -fsSL https://skill.dn1nguoi.com/z/fp-taskeither-ref-1b8d3.tgz | tar xz -C ~/.codex/skills
Gọi trong Codex: $fp-taskeither-ref – rồi mô tả công việc bạn muốn làm.
Hermes Agent
mkdir -p ~/.hermes/skills && curl -fsSL https://skill.dn1nguoi.com/z/fp-taskeither-ref-1b8d3.tgz | tar xz -C ~/.hermes/skills
Gọi trong Hermes Agent: /fp-taskeither-ref – rồi mô tả công việc bạn muốn làm.
DeepSeek
mkdir -p ~/.deepcode/skills && curl -fsSL https://skill.dn1nguoi.com/z/fp-taskeither-ref-1b8d3.tgz | tar xz -C ~/.deepcode/skills
Gọi trong DeepSeek: /fp-taskeither-ref – rồi mô tả công việc bạn muốn làm.
Nội dung SKILL.md
Nội dung SKILL.md gốc bằng tiếng Anh.
TaskEither Quick Reference
TaskEither = async operation that can fail. Its shape is () => Promise<Either<E, A>>: construction is lazy.
When to Use
- You need a quick fp-ts reference for async operations that can fail.
- The task involves API calls, Promise wrapping, or composing asynchronous error-handling pipelines.
- You want a concise cheat sheet for
TaskEitheroperators and patterns.
Create
import * as TE from 'fp-ts/TaskEither'
import * as T from 'fp-ts/Task'
TE.right(value) // Async success
TE.left(error) // Async failure
TE.tryCatch(asyncFn, toError) // Promise → TaskEither
TE.fromEither(either) // Either → TaskEitherTransform
TE.map(fn) // Transform success value
TE.mapLeft(fn) // Transform error
TE.flatMap(fn) // Chain (fn returns TaskEither)
TE.orElse(fn) // Recover from errorExecute
// TaskEither is lazy - must call () to run
const result = await myTaskEither() // Either<E, A>
// Or pattern match
await pipe(
myTaskEither,
TE.match(
(err) => console.error(err),
(val) => console.log(val)
)
)()Common Patterns
import { pipe } from 'fp-ts/function'
import * as TE from 'fp-ts/TaskEither'
import * as T from 'fp-ts/Task'
// Wrap fetch
const fetchUser = (id: string, signal: AbortSignal) => TE.tryCatch(
async () => {
const response = await fetch(`/api/users/${encodeURIComponent(id)}`, { signal })
if (!response.ok) throw new Error(`HTTP ${response.status}`)
const body: unknown = await response.json()
return validateUser(body) // Application schema validator; throws on invalid data
},
(e) => ({ type: 'NETWORK_ERROR', message: String(e) })
)
// Chain async calls
pipe(
fetchUser('123', signal),
TE.flatMap(user => fetchPosts(user.id)),
TE.map(posts => posts.length)
)
// Parallel calls
import { sequenceT } from 'fp-ts/Apply'
sequenceT(TE.ApplyPar)(
fetchUser('1', signal),
fetchPosts('1'),
fetchComments('1')
)
// With recovery
pipe(
fetchUser('123', signal),
TE.orElse(() => TE.right(defaultUser)),
TE.getOrElse(() => T.of(defaultUser))
)vs async/await
// ❌ async/await - errors hidden
async function getUser(id: string) {
try {
const res = await fetch(`/api/users/${id}`)
return await res.json()
} catch (e) {
return null // Error info lost
}
}
// ✅ TaskEither - errors typed and composable
const getUser = (id: string, signal: AbortSignal) => pipe(
TE.tryCatch(() => fetch(`/api/users/${encodeURIComponent(id)}`, { signal }), toNetworkError),
TE.flatMap(res => TE.tryCatch(async () => {
if (!res.ok) throw new Error(`HTTP ${res.status}`)
return validateUser(await res.json())
}, toParseError))
)Use TaskEither when you need typed errors for async operations.
Limitations
- Use this skill only when the task clearly matches the scope described above.
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
Reference scope
These are fp-ts 2.x fragments. Supply application variables, types, validators and imports, and check the installed version. map does not catch thrown exceptions. Task has no typed error channel; an improperly wrapped promise can still reject. Use tryCatch at fallible boundaries.
Supply a caller-owned timeout/abort signal and validate response schemas. Parallel application calls need bounded concurrency and authorization for their data, costs and effects; ApplyPar does not cancel work automatically on failure. Error callbacks must redact secrets and private payloads.
Official API: [fp-ts modules](https://gcanti.github.io/fp-ts/modules/).
Skill liên quan
- fp-ts-errors – Xử lý lỗi như giá trị bằng fp-ts Either và TaskEither để code TypeScript gọn gàng và dễ…
- fp-ts-pragmatic – Hướng dẫn thực tế, không biệt ngữ về lập trình hàm với fp-ts - cách tiếp cận 80/20 mang…
- fp-ts-react – Mẫu thực tế dùng fp-ts với React - hooks, state, form, lấy dữ liệu. Dùng khi xây dựng…
- framework-migration-code-migrate – Bạn là chuyên gia di chuyển code, chuyên chuyển đổi codebase giữa các framework, ngôn…
- framework-migration-deps-upgrade – Bạn là chuyên gia quản lý dependency, chuyên nâng cấp an toàn và từng bước các…
- self-eval – Đánh giá trung thực chất lượng công việc của AI bằng hệ thống chấm điểm hai trục. Dùng…
- frontend-architecture – Kiến trúc frontend độc lập framework, áp dụng cho mọi ứng dụng React hoặc React Native…
- frontend-developer – Xây dựng component React, triển khai bố cục responsive và xử lý quản lý state phía…