Sequencing Effects
In Effect TypeScript, sequential code requires Effect.gen(function* () { ... yield* ... }). In VibeLang, you just write an effect function and use try where async functions use await.
If you already know async/await, you already know 90% of VibeLang's control flow:
JavaScript async | VibeLang effect |
|---|---|
async function load() | effect function load() |
async (x: number) => ... | effect (x: number) => ... |
class C { async run() {} } | class C { effect run() {} } |
await promise | try effectValue |
throw err (untyped) | throw new MyError() (typed in E) |
Always write effect function name() at declaration level (effect name() outside a class or object literal is EK0101), and parenthesize typed or generic arrow parameters: effect (x: number) => x (EK0109). Plain function* keeps JavaScript's generator meaning; effect function* builds a Stream producer.
Step-by-step execution
Each try runs its Effect to completion before the next statement executes:
effect function invoice(quantity: number) {
const unitPrice = try Config.Finite("UNIT_PRICE")
const subtotal = quantity * unitPrice
try Console.log(`subtotal=${subtotal}`)
return subtotal
}
// Run in an effect function: try Console.log(try Effect.provide(invoice(3), ConfigProvider.layer(ConfigProvider.fromUnknown({ UNIT_PRICE: 4 }))))
// Output: subtotal=12
// Output: 12If Config.Finite("UNIT_PRICE") fails, execution short-circuits immediately — Console.log never runs, and Config.ConfigError flows into invoice's error channel E.
Calling other effect functions
effect function delayedLabel(name: string) {
try sleep("30 millis")
return name.toUpperCase()
}
effect function greeting() {
const label = try delayedLabel("sora")
return `Hello ${label}`
}
// Run in an effect function: try Console.log(try greeting())
// Output: Hello SORAWatch out for nested plain callbacks (
EK0501).tryis valid only inside aneffectfunction,effectmethod,effectarrow, oreffect { ... }block. A plain callback nested inside an effect function is still plain — mark iteffect(or useforEach/forloops) if it needs to run Effects.
Ordinary if, for, break, and continue
Normal JavaScript control flow works as expected inside effect functions:
effect function reportEven(values: ReadonlyArray<number>) {
let count = 0
for (const value of values) {
if (value < 0) break
if (value % 2 !== 0) continue
try Console.log(`even=${value}`)
count += 1
}
return count
}
// Run in an effect function: try Console.log(try reportEven([1, 2, 3, 4, -1, 6]))
// Output: even=2
// Output: even=4
// Output: 2A standard for...of runs iterations sequentially. To run iterations in parallel with a concurrency cap, use for concurrent(4) (const value of values) { ... } or Effect.forEach.
Failing early and narrowing types
Throwing a typed error exits the function immediately, so TypeScript's control-flow narrowing knows the error path cannot continue:
error MissingTitle
effect function titleLength(title: string | undefined) {
if (title === undefined) throw new MissingTitle()
return title.length
}
// Inferred: number!MissingTitle
effect function optionalTitle(title: string | undefined) {
return titleLength(title) catch () => 0
}
// Run in an effect function: try Console.log(try optionalTitle(undefined))
// Output: 0After the if (...) throw, title is narrowed to string. catch recovers expected failures; defects and interruption pass through while still running ensuring cleanup.
Methods keep this
An effect method on a class or object literal preserves this naturally — no Effect.gen(this, function* () { ... }) boilerplate:
class Prefixer {
constructor(readonly prefix: string) {}
effect format(value: string) {
try Console.log("formatting label")
return `${this.prefix}: ${value}`
}
}
effect function label() {
const prefixer = new Prefixer("item")
return try prefixer.format("notebook")
}
// Run in an effect function: try Console.log(try label())
// Output: formatting label
// Output: item: notebookComing from Effect
effect function and effect { ... } replace Effect.gen(function* () { ... }) and Effect.fn, and try replaces yield*. Effect.gen remains available as a library function when you need it.
Next: Building Pipelines — combine statements with the |> pipe operator.
The matching Effect page: effect.website/docs/v4/getting-started/using-generators