Loading...
Loading...
Explains basic effect usage and terms. Use when using effect in typescript.
npx skill4agent add tstelzer/skills effectEffect<Success, Error, Requirements>AER| Combinator | Input | Output |
|---|---|---|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
import { Effect } from "effect"
const ok = Effect.succeed(42)
const err = Effect.fail(new Error("oops"))
const sync = Effect.sync(() => console.log("hi"))
const trySync = Effect.try(() => JSON.parse(str))
const tryAsync = Effect.tryPromise(() => fetch(url))suspend| Runner | Output |
|---|---|
| |
| |
| |
Effect.runSync(program) // sync only
Effect.runPromise(program) // async
Effect.runFork(program) // background fibermain// BAD: runs effect mid-chain
async function getUser(id: string) {
return Effect.runPromise(fetchUser(id))
}
// GOOD: return the effect, run at edge
const getUser = (id: string): Effect.Effect<User, Error> =>
fetchUser(id)| Operator | Purpose |
|---|---|
| Transform success value: |
| Chain effects: |
| Flexible chain (value, fn, Effect, Promise) |
| Side effect, keeps original value |
| Combine effects into tuple/struct |
import { Effect, pipe } from "effect"
const program = pipe(
fetchAmount,
Effect.map((n) => n * 2),
Effect.flatMap((n) => applyDiscount(n)),
Effect.tap((n) => Console.log(`Result: ${n}`))
)
// or with .pipe method
const program2 = fetchAmount.pipe(
Effect.andThen((n) => n * 2),
Effect.andThen((n) => applyDiscount(n))
)
// combine multiple effects
const both = Effect.all([effectA, effectB])
const struct = Effect.all({ a: effectA, b: effectB })const program = Effect.gen(function* () {
const a = yield* effectA
const b = yield* effectB
return a + b
})Effect.gen(function* () {
const user = yield* getUser(id)
if (!user) {
return yield* Effect.fail("not found")
}
return user.name
})Effect.genifelseforwhilepipeimport { Effect, Data } from "effect"
class NotFound extends Data.TaggedError("NotFound")<{
id: string
}> {}
class Unauthorized extends Data.TaggedError("Unauthorized")<{}> {}
const program: Effect.Effect<User, NotFound | Unauthorized> =
Effect.gen(function* () {
// ...
yield* Effect.fail(new NotFound({ id }))
})
// Handle specific errors
program.pipe(
Effect.catchTag("NotFound", (e) => Effect.succeed(null)),
Effect.catchTag("Unauthorized", () => Effect.fail("denied"))
)Effect.gen(function* () {
yield* task1 // runs
yield* Effect.fail(e) // fails here
yield* task2 // never runs
})Effect.all{ mode: "either" }