<K = unknown>(options: {
readonly permits: number
}): PartitionedSemaphore<K>Constructs a PartitionedSemaphore synchronously, outside of Effect.
When to use
Use when you need to construct a partitioned semaphore synchronously outside
an Effect workflow.
Details
Negative permit counts are clamped to 0. Non-finite permit counts create
an unbounded semaphore whose acquire and release operations complete
immediately.
export const const makeUnsafe: <K = unknown>(options: {
readonly permits: number
}) => PartitionedSemaphore<K>
Constructs a PartitionedSemaphore synchronously, outside of Effect.
When to use
Use when you need to construct a partitioned semaphore synchronously outside
an Effect workflow.
Details
Negative permit counts are clamped to 0. Non-finite permit counts create
an unbounded semaphore whose acquire and release operations complete
immediately.
makeUnsafe = <function (type parameter) K in <K = unknown>(options: {
readonly permits: number;
}): PartitionedSemaphore<K>
K = unknown>(options: {
readonly permits: number
}
options: {
readonly permits: numberpermits: number
}): interface PartitionedSemaphore<in K>A PartitionedSemaphore controls access to a shared permit pool while
tracking waiters by partition key.
When to use
Use to coordinate shared permits across partition keys so waiting groups make
progress without one group monopolizing the pool.
Details
Waiting permits are distributed across partitions in round-robin order.
PartitionedSemaphore<function (type parameter) K in <K = unknown>(options: {
readonly permits: number;
}): PartitionedSemaphore<K>
K> => {
const const maxPermits: numbermaxPermits = var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.max(...values: number[]): numberReturns the larger of a set of supplied numeric expressions.
max(0, options: {
readonly permits: number
}
options.permits: numberpermits)
if (!var Number: NumberConstructorAn object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number.NumberConstructor.isFinite(number: unknown): booleanReturns true if passed value is finite.
Unlike the global isFinite, Number.isFinite doesn't forcibly convert the parameter to a
number. Only finite values of the type number, result in true.
isFinite(const maxPermits: numbermaxPermits)) {
return {
[const PartitionedTypeId: PartitionedTypeIdRuntime type identifier used to mark values that implement
PartitionedSemaphore.
Details
This marker is part of the runtime representation of partitioned semaphore
values.
Literal type of the PartitionedSemaphore runtime type identifier.
When to use
Use to type fields that store the exact PartitionedSemaphore runtime marker.
Details
Use this type when declaring fields that must contain the exact
PartitionedTypeId marker value.
PartitionedTypeId]: const PartitionedTypeId: PartitionedTypeIdRuntime type identifier used to mark values that implement
PartitionedSemaphore.
Details
This marker is part of the runtime representation of partitioned semaphore
values.
Literal type of the PartitionedSemaphore runtime type identifier.
When to use
Use to type fields that store the exact PartitionedSemaphore runtime marker.
Details
Use this type when declaring fields that must contain the exact
PartitionedTypeId marker value.
PartitionedTypeId,
PartitionedSemaphore<in K>.capacity: numbercapacity: const maxPermits: numbermaxPermits,
PartitionedSemaphore<in K>.available: Effect.Effect<number, never, never>(property) PartitionedSemaphore<in K>.available: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
available: import EffectEffect.const succeed: <A>(value: A) => Effect<A>Creates an Effect that always succeeds with a given value.
When to use
Use when an effect should complete successfully with a specific value without any errors
or external dependencies.
Example (Creating a successful effect)
import { Effect } from "effect"
// Creating an effect that represents a successful scenario
//
// ┌─── Effect<number, never, never>
// ▼
const success = Effect.succeed(42)
succeed(const maxPermits: numbermaxPermits),
PartitionedSemaphore<K>.take: (key: K, permits: number) => Effect.Effect<void>take: () => import EffectEffect.const void: Effect.Effect<void, never, never>(alias) const void: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
Returns an effect that succeeds with void.
void,
PartitionedSemaphore<K>.release: (permits: number) => Effect.Effect<number>release: () => import EffectEffect.const succeed: <A>(value: A) => Effect<A>Creates an Effect that always succeeds with a given value.
When to use
Use when an effect should complete successfully with a specific value without any errors
or external dependencies.
Example (Creating a successful effect)
import { Effect } from "effect"
// Creating an effect that represents a successful scenario
//
// ┌─── Effect<number, never, never>
// ▼
const success = Effect.succeed(42)
succeed(const maxPermits: numbermaxPermits),
PartitionedSemaphore<K>.withPermits: (key: K, permits: number) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>withPermits: () => (effect: Effect.Effect<A, E, R>(parameter) effect: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
effect) => effect: Effect.Effect<A, E, R>(parameter) effect: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
effect,
PartitionedSemaphore<K>.withPermit: (key: K) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>withPermit: () => (effect: Effect.Effect<A, E, R>(parameter) effect: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
effect) => effect: Effect.Effect<A, E, R>(parameter) effect: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
effect,
PartitionedSemaphore<K>.withPermitsIfAvailable: (permits: number) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<Option.Option<A>, E, R>withPermitsIfAvailable: () => (effect: Effect.Effect<A, E, R>(parameter) effect: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
effect) => import EffectEffect.const asSome: <A, E, R>(
self: Effect<A, E, R>
) => Effect<Option<A>, E, R>
Maps the success value of an Effect to Some, preserving failures.
Example (Wrapping success in Option.some)
import { Effect } from "effect"
const program = Effect.asSome(Effect.succeed(42))
Effect.runPromise(program).then(console.log)
// { _id: 'Option', _tag: 'Some', value: 42 }
asSome(effect: Effect.Effect<A, E, R>(parameter) effect: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
effect)
}
}
let let totalPermits: numbertotalPermits = const maxPermits: numbermaxPermits
let let waitingPermits: numberwaitingPermits = 0
type type Waiter = {
permits: number
readonly resume: () => void
}
Waiter = {
permits: numberpermits: number
readonly resume: () => voidresume: () => void
}
const const partitions: MutableHashMap.MutableHashMap<
K,
Set<Waiter>
>
const partitions: {
backing: Map<K, V>;
buckets: Map<number, NonEmptyArray<K>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
partitions = import MutableHashMapMutableHashMap.const empty: <K, V>() => MutableHashMap<
K,
V
>
Creates an empty MutableHashMap.
When to use
Use to create a fresh mutable map before adding entries over time.
Details
Each call returns a new empty map instance.
Example (Creating an empty map)
import { MutableHashMap } from "effect"
const map = MutableHashMap.empty<string, number>()
// Add some entries
MutableHashMap.set(map, "key1", 42)
MutableHashMap.set(map, "key2", 100)
console.log(MutableHashMap.size(map)) // 2
empty<function (type parameter) K in <K = unknown>(options: {
readonly permits: number;
}): PartitionedSemaphore<K>
K, interface Set<T>Set<type Waiter = {
permits: number
readonly resume: () => void
}
Waiter>>()
let let iterator: Iterator<
[K, Set<Waiter>],
any,
any
>
iterator = const partitions: MutableHashMap.MutableHashMap<
K,
Set<Waiter>
>
const partitions: {
backing: Map<K, V>;
buckets: Map<number, NonEmptyArray<K>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
partitions[var Symbol: SymbolConstructorSymbol.SymbolConstructor.iterator: typeof Symbol.iteratorA method that returns the default iterator for an object. Called by the semantics of the
for-of statement.
iterator]()
const const releaseUnsafe: (
permits: number
) => number
releaseUnsafe = (permits: numberpermits: number): number => {
while (permits: numberpermits > 0) {
if (let waitingPermits: numberwaitingPermits === 0) {
let totalPermits: numbertotalPermits = var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.min(...values: number[]): numberReturns the smaller of a set of supplied numeric expressions.
min(const maxPermits: numbermaxPermits, let totalPermits: numbertotalPermits + permits: numberpermits)
return let totalPermits: numbertotalPermits
}
let let state: IteratorResult<
[K, Set<Waiter>],
any
>
state = let iterator: Iterator<
[K, Set<Waiter>],
any,
any
>
iterator.Iterator<[K, Set<Waiter>], any, any>.next(...[value]: [] | [any]): IteratorResult<[K, Set<Waiter>], any>next()
if (let state: IteratorResult<
[K, Set<Waiter>],
any
>
state.done?: boolean | undefineddone) {
let iterator: Iterator<
[K, Set<Waiter>],
any,
any
>
iterator = const partitions: MutableHashMap.MutableHashMap<
K,
Set<Waiter>
>
const partitions: {
backing: Map<K, V>;
buckets: Map<number, NonEmptyArray<K>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
partitions[var Symbol: SymbolConstructorSymbol.SymbolConstructor.iterator: typeof Symbol.iteratorA method that returns the default iterator for an object. Called by the semantics of the
for-of statement.
iterator]()
let state: IteratorResult<
[K, Set<Waiter>],
any
>
state = let iterator: Iterator<
[K, Set<Waiter>],
any,
any
>
iterator.Iterator<[K, Set<Waiter>], any, any>.next(...[value]: [] | [any]): IteratorResult<[K, Set<Waiter>], any>next()
if (let state: IteratorResult<
[K, Set<Waiter>],
any
>
state.done?: boolean | undefineddone) {
return let totalPermits: numbertotalPermits
}
}
const const waiter: Waiter | undefinedwaiter = let state: IteratorYieldResult<
[K, Set<Waiter>]
>
state.IteratorYieldResult<[K, Set<Waiter>]>.value: [K, Set<Waiter>](property) IteratorYieldResult<[K, Set<Waiter>]>.value: {
0: K;
1: Set<Waiter>;
length: 2;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
pop: () => K | Set<Waiter> | undefined;
push: (...items: Array<K | Set<Waiter>>) => number;
concat: { (...items: Array<ConcatArray<K | Set<Waiter>>>): Array<K | Set<Waiter>>; (...items: Array<K | Set<Waiter> | ConcatArray<K | Set<Waiter>>>): Array<K | Set<Waiter>> };
join: (separator?: string) => string;
reverse: () => Array<K | Set<Waiter>>;
shift: () => K | Set<Waiter> | undefined;
slice: (start?: number, end?: number) => Array<K | Set<Waiter>>;
sort: (compareFn?: ((a: K | Set<Waiter>, b: K | Set<Waiter>) => number) | undefined) => [K, Set<Waiter>];
splice: { (start: number, deleteCount?: number): Array<K | Set<Waiter>>; (start: number, deleteCount: number, ...items: Array<K | Set<Waiter>>): Array<K | Set<Waiter>> };
unshift: (...items: Array<K | Set<Waiter>>) => number;
indexOf: (searchElement: K | Set<Waiter>, fromIndex?: number) => number;
lastIndexOf: (searchElement: K | Set<Waiter>, fromIndex?: number) => number;
every: { (predicate: (value: K | Set<Waiter>, index: number, array: Array<K | Set<Waiter>>) => value is S, thisArg?: any): this is S[]; (predicate: (value: K | Set<Waiter>, index: number, array: Array<K | Set<Waiter>>) => unknown, thisArg?: any):…;
some: (predicate: (value: K | Set<Waiter>, index: number, array: Array<K | Set<Waiter>>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: K | Set<Waiter>, index: number, array: Array<K | Set<Waiter>>) => void, thisArg?: any) => void;
map: (callbackfn: (value: K | Set<Waiter>, index: number, array: Array<K | Set<Waiter>>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: K | Set<Waiter>, index: number, array: Array<K | Set<Waiter>>) => value is S, thisArg?: any): Array<S>; (predicate: (value: K | Set<Waiter>, index: number, array: Array<K | Set<Waiter>>) => unknown, thisArg?: any): Ar…;
reduce: { (callbackfn: (previousValue: K | Set<Waiter>, currentValue: K | Set<Waiter>, currentIndex: number, array: Array<K | Set<Waiter>>) => K | Set<Waiter>): K | Set<Waiter>; (callbackfn: (previousValue: K | Set<Waiter>, currentValue: K | Set<W…;
reduceRight: { (callbackfn: (previousValue: K | Set<Waiter>, currentValue: K | Set<Waiter>, currentIndex: number, array: Array<K | Set<Waiter>>) => K | Set<Waiter>): K | Set<Waiter>; (callbackfn: (previousValue: K | Set<Waiter>, currentValue: K | Set<W…;
find: { (predicate: (value: K | Set<Waiter>, index: number, obj: Array<K | Set<Waiter>>) => value is S, thisArg?: any): S | undefined; (predicate: (value: K | Set<Waiter>, index: number, obj: Array<K | Set<Waiter>>) => unknown, thisArg?: any): K…;
findIndex: (predicate: (value: K | Set<Waiter>, index: number, obj: Array<K | Set<Waiter>>) => unknown, thisArg?: any) => number;
fill: (value: K | Set<Waiter>, start?: number, end?: number) => [K, Set<Waiter>];
copyWithin: (target: number, start: number, end?: number) => [K, Set<Waiter>];
entries: () => ArrayIterator<[number, K | Set<Waiter>]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<K | Set<Waiter>>;
includes: (searchElement: K | Set<Waiter>, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: K | Set<Waiter>, index: number, array: Array<K | Set<Waiter>>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => K | Set<Waiter> | undefined;
findLast: { (predicate: (value: K | Set<Waiter>, index: number, array: Array<K | Set<Waiter>>) => value is S, thisArg?: any): S | undefined; (predicate: (value: K | Set<Waiter>, index: number, array: Array<K | Set<Waiter>>) => unknown, thisArg?: any…;
findLastIndex: (predicate: (value: K | Set<Waiter>, index: number, array: Array<K | Set<Waiter>>) => unknown, thisArg?: any) => number;
toReversed: () => Array<K | Set<Waiter>>;
toSorted: (compareFn?: ((a: K | Set<Waiter>, b: K | Set<Waiter>) => number) | undefined) => Array<K | Set<Waiter>>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<K | Set<Waiter>>): Array<K | Set<Waiter>>; (start: number, deleteCount?: number): Array<K | Set<Waiter>> };
with: (index: number, value: K | Set<Waiter>) => Array<K | Set<Waiter>>;
}
value[1].Set<Waiter>.values(): SetIterator<Waiter>Returns an iterable of values in the set.
values().Iterator<Waiter, undefined, unknown>.next(...[value]: [] | [unknown]): IteratorResult<Waiter, undefined>next().value: Waiter | undefinedvalue
if (const waiter: Waiter | undefinedwaiter === var undefinedundefined) {
continue
}
const waiter: Waiterconst waiter: {
permits: number;
resume: () => void;
}
waiter.permits: numberpermits -= 1
let waitingPermits: numberwaitingPermits -= 1
if (const waiter: Waiterconst waiter: {
permits: number;
resume: () => void;
}
waiter.permits: numberpermits === 0) {
const waiter: Waiterconst waiter: {
permits: number;
resume: () => void;
}
waiter.resume: () => voidresume()
}
permits: numberpermits -= 1
}
return let totalPermits: numbertotalPermits
}
const const take: (
key: K,
permits: number
) => Effect.Effect<void>
take = (key: K = unknownkey: function (type parameter) K in <K = unknown>(options: {
readonly permits: number;
}): PartitionedSemaphore<K>
K, permits: numberpermits: number): import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<void> => {
if (permits: numberpermits <= 0) {
return import EffectEffect.const void: Effect.Effect<void, never, never>(alias) const void: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
Returns an effect that succeeds with void.
void
}
return import EffectEffect.const callback: <A, E = never, R = never>(
register: (
this: Scheduler,
resume: (effect: Effect<A, E, R>) => void,
signal: AbortSignal
) => void | Effect<void, never, R>
) => Effect<A, E, R>
Creates an Effect from a callback-based asynchronous API.
When to use
Use when you need to integrate APIs that complete through callbacks instead
of returning a Promise.
Details
The registration function receives a resume callback and, when requested,
an AbortSignal. Call resume at most once with the effect that should
complete the fiber; later calls are ignored. Return an optional cleanup
effect from the registration function to run if the fiber is interrupted.
Example (Integrating callback APIs)
import { Effect } from "effect"
const delay = (ms: number) =>
Effect.callback<void>((resume) => {
const timeoutId = setTimeout(() => {
resume(Effect.void)
}, ms)
// Cleanup function for interruption
return Effect.sync(() => clearTimeout(timeoutId))
})
const program = delay(1000)
callback<void>((resume: (
effect: Effect.Effect<void, never, never>
) => void
resume) => {
if (const maxPermits: numbermaxPermits < permits: numberpermits) {
resume: (
effect: Effect.Effect<void, never, never>
) => void
resume(import EffectEffect.const never: Effect<never>const never: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
Returns an effect that will never produce anything. The moral equivalent of
while(true) {}, only without the wasted CPU cycles.
Example (Creating a never-ending effect)
import { Effect } from "effect"
// This effect will never complete
const program = Effect.never
// This will run forever (or until interrupted)
// Effect.runPromise(program) // Never resolves
// Use with timeout for practical applications
const timedProgram = Effect.timeout(program, "1 second")
never)
return
}
if (let totalPermits: numbertotalPermits >= permits: numberpermits) {
let totalPermits: numbertotalPermits -= permits: numberpermits
resume: (
effect: Effect.Effect<void, never, never>
) => void
resume(import EffectEffect.const void: Effect.Effect<void, never, never>(alias) const void: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
Returns an effect that succeeds with void.
void)
return
}
const const needed: numberneeded = permits: numberpermits - let totalPermits: numbertotalPermits
const const taken: numbertaken = permits: numberpermits - const needed: numberneeded
if (let totalPermits: numbertotalPermits > 0) {
let totalPermits: numbertotalPermits = 0
}
let waitingPermits: numberwaitingPermits += const needed: numberneeded
const const waiters: Set<Waiter>waiters = import OptionOption.const getOrElse: {
<B>(onNone: LazyArg<B>): <A>(
self: Option<A>
) => B | A
<A, B>(self: Option<A>, onNone: LazyArg<B>):
| A
| B
}
getOrElse(
import MutableHashMapMutableHashMap.const get: {
<K>(key: K): <V>(
self: MutableHashMap<K, V>
) => Option.Option<V>
<K, V>(
self: MutableHashMap<K, V>,
key: K
): Option.Option<V>
}
get(const partitions: MutableHashMap.MutableHashMap<
K,
Set<Waiter>
>
const partitions: {
backing: Map<K, V>;
buckets: Map<number, NonEmptyArray<K>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
partitions, key: K = unknownkey),
() => {
const const set: Set<Waiter>set = new var Set: SetConstructor
new <Waiter>(iterable?: Iterable<Waiter> | null | undefined) => Set<Waiter> (+1 overload)
Set<type Waiter = {
permits: number
readonly resume: () => void
}
Waiter>()
import MutableHashMapMutableHashMap.const set: {
<K, V>(key: K, value: V): (
self: MutableHashMap<K, V>
) => MutableHashMap<K, V>
<K, V>(
self: MutableHashMap<K, V>,
key: K,
value: V
): MutableHashMap<K, V>
}
set(const partitions: MutableHashMap.MutableHashMap<
K,
Set<Waiter>
>
const partitions: {
backing: Map<K, V>;
buckets: Map<number, NonEmptyArray<K>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
partitions, key: K = unknownkey, const set: Set<Waiter>set)
return const set: Set<Waiter>set
}
)
const const entry: Waiterconst entry: {
permits: number;
resume: () => void;
}
entry: type Waiter = {
permits: number
readonly resume: () => void
}
Waiter = {
permits: numberpermits: const needed: numberneeded,
resume: () => voidresume: () => {
const cleanup: () => voidcleanup()
resume: (
effect: Effect.Effect<void, never, never>
) => void
resume(import EffectEffect.const void: Effect.Effect<void, never, never>(alias) const void: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
Returns an effect that succeeds with void.
void)
}
}
const const cleanup: () => voidcleanup = () => {
const waiters: Set<Waiter>waiters.Set<Waiter>.delete(value: Waiter): booleanRemoves a specified value from the Set.
delete(const entry: Waiterconst entry: {
permits: number;
resume: () => void;
}
entry)
if (const waiters: Set<Waiter>waiters.Set<Waiter>.size: numbersize === 0) {
import MutableHashMapMutableHashMap.const remove: {
<K>(key: K): <V>(
self: MutableHashMap<K, V>
) => MutableHashMap<K, V>
<K, V>(
self: MutableHashMap<K, V>,
key: K
): MutableHashMap<K, V>
}
remove(const partitions: MutableHashMap.MutableHashMap<
K,
Set<Waiter>
>
const partitions: {
backing: Map<K, V>;
buckets: Map<number, NonEmptyArray<K>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
partitions, key: K = unknownkey)
}
}
const waiters: Set<Waiter>waiters.Set<Waiter>.add(value: Waiter): Set<Waiter>Appends a new element with a specified value to the end of the Set.
add(const entry: Waiterconst entry: {
permits: number;
resume: () => void;
}
entry)
return import EffectEffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect<A>
Creates an Effect that represents a synchronous side-effectful computation.
When to use
Use when you need to wrap a synchronous side-effectful operation that is not
expected to throw.
Details
The provided function is evaluated lazily when the effect runs.
Gotchas
The function must not throw. If it throws, the thrown value is treated as a
defect, not as a typed failure. Use try when throwing is expected.
Example (Capturing synchronous logging in an Effect)
import { Effect } from "effect"
const log = (message: string) =>
Effect.sync(() => {
console.log(message) // side effect
})
// ┌─── Effect<void, never, never>
// ▼
const program = log("Hello, World!")
sync(() => {
const cleanup: () => voidcleanup()
let waitingPermits: numberwaitingPermits -= const entry: Waiterconst entry: {
permits: number;
resume: () => void;
}
entry.permits: numberpermits
if (const taken: numbertaken > 0) {
const releaseUnsafe: (
permits: number
) => number
releaseUnsafe(const taken: numbertaken)
}
})
})
}
const const withPermits: (
key: K,
permits: number
) => <A, E, R>(
effect: Effect.Effect<A, E, R>
) => Effect.Effect<A, E, R>
withPermits =
(key: K = unknownkey: function (type parameter) K in <K = unknown>(options: {
readonly permits: number;
}): PartitionedSemaphore<K>
K, permits: numberpermits: number) => <function (type parameter) A in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>R>(effect: Effect.Effect<A, E, R>(parameter) effect: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
effect: import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<function (type parameter) A in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>R>): import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<function (type parameter) A in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>R> => {
if (permits: numberpermits <= 0) {
return effect: Effect.Effect<A, E, R>(parameter) effect: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
effect
}
const const takePermits: Effect.Effect<
void,
never,
never
>
const takePermits: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
takePermits = const take: (
key: K,
permits: number
) => Effect.Effect<void>
take(key: K = unknownkey, permits: numberpermits)
return import EffectEffect.const uninterruptibleMask: <A, E, R>(
f: (
restore: <AX, EX, RX>(
effect: Effect<AX, EX, RX>
) => Effect<AX, EX, RX>
) => Effect<A, E, R>
) => Effect<A, E, R>
Disables interruption and provides a restore function to restore the
interruptible state within the effect.
Example (Restoring interruption in protected regions)
import { Console, Effect } from "effect"
const program = Effect.uninterruptibleMask((restore) =>
Effect.gen(function*() {
yield* Console.log("Uninterruptible phase...")
yield* Effect.sleep("1 second")
// Restore interruptibility for this part
yield* restore(
Effect.gen(function*() {
yield* Console.log("Interruptible phase...")
yield* Effect.sleep("2 seconds")
})
)
yield* Console.log("Back to uninterruptible")
})
)
uninterruptibleMask((restore: <AX, EX, RX>(
effect: Effect<AX, EX, RX>
) => Effect<AX, EX, RX>
restore) =>
import EffectEffect.const flatMap: {
<A, B, E1, R1>(
f: (a: A) => Effect<B, E1, R1>
): <E, R>(
self: Effect<A, E, R>
) => Effect<B, E1 | E, R1 | R>
<A, E, R, B, E1, R1>(
self: Effect<A, E, R>,
f: (a: A) => Effect<B, E1, R1>
): Effect<B, E | E1, R | R1>
}
flatMap(
restore: <AX, EX, RX>(
effect: Effect<AX, EX, RX>
) => Effect<AX, EX, RX>
restore(const takePermits: Effect.Effect<
void,
never,
never
>
const takePermits: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
takePermits),
() =>
import EffectEffect.const ensuring: {
<X, R1>(finalizer: Effect<X, never, R1>): <
A,
E,
R
>(
self: Effect<A, E, R>
) => Effect<A, E, R1 | R>
<A, E, R, X, R1>(
self: Effect<A, E, R>,
finalizer: Effect<X, never, R1>
): Effect<A, E, R1 | R>
}
ensuring(
restore: <AX, EX, RX>(
effect: Effect<AX, EX, RX>
) => Effect<AX, EX, RX>
restore(effect: Effect.Effect<A, E, R>(parameter) effect: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
effect),
import EffectEffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect<A>
Creates an Effect that represents a synchronous side-effectful computation.
When to use
Use when you need to wrap a synchronous side-effectful operation that is not
expected to throw.
Details
The provided function is evaluated lazily when the effect runs.
Gotchas
The function must not throw. If it throws, the thrown value is treated as a
defect, not as a typed failure. Use try when throwing is expected.
Example (Capturing synchronous logging in an Effect)
import { Effect } from "effect"
const log = (message: string) =>
Effect.sync(() => {
console.log(message) // side effect
})
// ┌─── Effect<void, never, never>
// ▼
const program = log("Hello, World!")
sync(() => {
const releaseUnsafe: (
permits: number
) => number
releaseUnsafe(permits: numberpermits)
})
)
)
)
}
const const tryTake: (
permits: number
) => boolean
tryTake = (permits: numberpermits: number): boolean => {
if (permits: numberpermits <= 0) {
return true
}
if (const maxPermits: numbermaxPermits < permits: numberpermits || let totalPermits: numbertotalPermits < permits: numberpermits) {
return false
}
let totalPermits: numbertotalPermits -= permits: numberpermits
return true
}
return {
[const PartitionedTypeId: PartitionedTypeIdRuntime type identifier used to mark values that implement
PartitionedSemaphore.
Details
This marker is part of the runtime representation of partitioned semaphore
values.
Literal type of the PartitionedSemaphore runtime type identifier.
When to use
Use to type fields that store the exact PartitionedSemaphore runtime marker.
Details
Use this type when declaring fields that must contain the exact
PartitionedTypeId marker value.
PartitionedTypeId]: const PartitionedTypeId: PartitionedTypeIdRuntime type identifier used to mark values that implement
PartitionedSemaphore.
Details
This marker is part of the runtime representation of partitioned semaphore
values.
Literal type of the PartitionedSemaphore runtime type identifier.
When to use
Use to type fields that store the exact PartitionedSemaphore runtime marker.
Details
Use this type when declaring fields that must contain the exact
PartitionedTypeId marker value.
PartitionedTypeId,
PartitionedSemaphore<in K>.capacity: numbercapacity: const maxPermits: numbermaxPermits,
PartitionedSemaphore<in K>.available: Effect.Effect<number, never, never>(property) PartitionedSemaphore<in K>.available: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
available: import EffectEffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect<A>
Creates an Effect that represents a synchronous side-effectful computation.
When to use
Use when you need to wrap a synchronous side-effectful operation that is not
expected to throw.
Details
The provided function is evaluated lazily when the effect runs.
Gotchas
The function must not throw. If it throws, the thrown value is treated as a
defect, not as a typed failure. Use try when throwing is expected.
Example (Capturing synchronous logging in an Effect)
import { Effect } from "effect"
const log = (message: string) =>
Effect.sync(() => {
console.log(message) // side effect
})
// ┌─── Effect<void, never, never>
// ▼
const program = log("Hello, World!")
sync(() => let totalPermits: numbertotalPermits),
PartitionedSemaphore<K>.take: (key: K, permits: number) => Effect.Effect<void>take,
PartitionedSemaphore<K>.release: (permits: number) => Effect.Effect<number>release: (permits: numberpermits) => import EffectEffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect<A>
Creates an Effect that represents a synchronous side-effectful computation.
When to use
Use when you need to wrap a synchronous side-effectful operation that is not
expected to throw.
Details
The provided function is evaluated lazily when the effect runs.
Gotchas
The function must not throw. If it throws, the thrown value is treated as a
defect, not as a typed failure. Use try when throwing is expected.
Example (Capturing synchronous logging in an Effect)
import { Effect } from "effect"
const log = (message: string) =>
Effect.sync(() => {
console.log(message) // side effect
})
// ┌─── Effect<void, never, never>
// ▼
const program = log("Hello, World!")
sync(() => const releaseUnsafe: (
permits: number
) => number
releaseUnsafe(permits: numberpermits)),
PartitionedSemaphore<K>.withPermits: (key: K, permits: number) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>withPermits,
PartitionedSemaphore<K>.withPermit: (key: K) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>withPermit: (key: K = unknownkey) => const withPermits: (
key: K,
permits: number
) => <A, E, R>(
effect: Effect.Effect<A, E, R>
) => Effect.Effect<A, E, R>
withPermits(key: K = unknownkey, 1),
PartitionedSemaphore<K>.withPermitsIfAvailable: (permits: number) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<Option.Option<A>, E, R>withPermitsIfAvailable:
(permits: numberpermits) => <function (type parameter) A in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<Option.Option<A>, E, R>A, function (type parameter) E in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<Option.Option<A>, E, R>E, function (type parameter) R in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<Option.Option<A>, E, R>R>(effect: Effect.Effect<A, E, R>(parameter) effect: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
effect: import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<function (type parameter) A in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<Option.Option<A>, E, R>A, function (type parameter) E in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<Option.Option<A>, E, R>E, function (type parameter) R in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<Option.Option<A>, E, R>R>): import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) A in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<Option.Option<A>, E, R>A>, function (type parameter) E in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<Option.Option<A>, E, R>E, function (type parameter) R in <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<Option.Option<A>, E, R>R> => {
if (permits: numberpermits <= 0) {
return import EffectEffect.const asSome: <A, E, R>(
self: Effect<A, E, R>
) => Effect<Option<A>, E, R>
Maps the success value of an Effect to Some, preserving failures.
Example (Wrapping success in Option.some)
import { Effect } from "effect"
const program = Effect.asSome(Effect.succeed(42))
Effect.runPromise(program).then(console.log)
// { _id: 'Option', _tag: 'Some', value: 42 }
asSome(effect: Effect.Effect<A, E, R>(parameter) effect: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
effect)
}
return import EffectEffect.const suspend: <A, E, R>(
effect: LazyArg<Effect<A, E, R>>
) => Effect<A, E, R>
Creates an Effect lazily, delaying construction until it is needed.
When to use
Use when you need to defer the evaluation of an effect until it is required.
Details
suspend takes a thunk that represents an effect and delays creating it
until the suspended effect is evaluated. This is useful for optimizing
expensive computations, managing circular dependencies such as recursive
functions, and helping TypeScript unify return types when branches construct
different effects. Any side effects or scoped captures inside the thunk are
re-executed on each invocation.
Example (Lazily evaluating side effects)
import { Effect } from "effect"
let i = 0
const bad = Effect.succeed(i++)
const good = Effect.suspend(() => Effect.succeed(i++))
console.log(Effect.runSync(bad)) // Output: 0
console.log(Effect.runSync(bad)) // Output: 0
console.log(Effect.runSync(good)) // Output: 1
console.log(Effect.runSync(good)) // Output: 2
Example (Suspending recursive Fibonacci evaluation)
import { Effect } from "effect"
const blowsUp = (n: number): Effect.Effect<number> =>
n < 2
? Effect.succeed(1)
: Effect.zipWith(blowsUp(n - 1), blowsUp(n - 2), (a, b) => a + b)
// console.log(Effect.runSync(blowsUp(32)))
// crash: JavaScript heap out of memory
const allGood = (n: number): Effect.Effect<number> =>
n < 2
? Effect.succeed(1)
: Effect.zipWith(
Effect.suspend(() => allGood(n - 1)),
Effect.suspend(() => allGood(n - 2)),
(a, b) => a + b
)
console.log(Effect.runSync(allGood(32)))
// Output: 3524578
Example (Helping TypeScript infer recursive effect types)
import { Effect } from "effect"
// Without suspend, TypeScript may struggle with type inference.
// Inferred type:
// (a: number, b: number) =>
// Effect<never, Error, never> | Effect<number, never, never>
const withoutSuspend = (a: number, b: number) =>
b === 0
? Effect.fail(new Error("Cannot divide by zero"))
: Effect.succeed(a / b)
// Using suspend to unify return types.
// Inferred type:
// (a: number, b: number) => Effect<number, Error, never>
const withSuspend = (a: number, b: number) =>
Effect.suspend(() =>
b === 0
? Effect.fail(new Error("Cannot divide by zero"))
: Effect.succeed(a / b)
)
suspend(() => {
if (!const tryTake: (
permits: number
) => boolean
tryTake(permits: numberpermits)) {
return import EffectEffect.const succeed: <A>(value: A) => Effect<A>Creates an Effect that always succeeds with a given value.
When to use
Use when an effect should complete successfully with a specific value without any errors
or external dependencies.
Example (Creating a successful effect)
import { Effect } from "effect"
// Creating an effect that represents a successful scenario
//
// ┌─── Effect<number, never, never>
// ▼
const success = Effect.succeed(42)
succeed(import OptionOption.const none: <A = never>() => Option<A>Creates an Option representing the absence of a value.
When to use
Use to represent a missing or uninitialized value, such as returning "no
result" from a function.
Details
- Returns
Option<never>, which is a subtype of Option<A> for any A
- Always returns the same singleton instance
Example (Creating an empty Option)
import { Option } from "effect"
// ┌─── Option<never>
// ▼
const noValue = Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
none())
}
return import EffectEffect.const ensuring: {
<X, R1>(finalizer: Effect<X, never, R1>): <
A,
E,
R
>(
self: Effect<A, E, R>
) => Effect<A, E, R1 | R>
<A, E, R, X, R1>(
self: Effect<A, E, R>,
finalizer: Effect<X, never, R1>
): Effect<A, E, R1 | R>
}
ensuring(
import EffectEffect.const asSome: <A, E, R>(
self: Effect<A, E, R>
) => Effect<Option<A>, E, R>
Maps the success value of an Effect to Some, preserving failures.
Example (Wrapping success in Option.some)
import { Effect } from "effect"
const program = Effect.asSome(Effect.succeed(42))
Effect.runPromise(program).then(console.log)
// { _id: 'Option', _tag: 'Some', value: 42 }
asSome(effect: Effect.Effect<A, E, R>(parameter) effect: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
effect),
import EffectEffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect<A>
Creates an Effect that represents a synchronous side-effectful computation.
When to use
Use when you need to wrap a synchronous side-effectful operation that is not
expected to throw.
Details
The provided function is evaluated lazily when the effect runs.
Gotchas
The function must not throw. If it throws, the thrown value is treated as a
defect, not as a typed failure. Use try when throwing is expected.
Example (Capturing synchronous logging in an Effect)
import { Effect } from "effect"
const log = (message: string) =>
Effect.sync(() => {
console.log(message) // side effect
})
// ┌─── Effect<void, never, never>
// ▼
const program = log("Hello, World!")
sync(() => {
const releaseUnsafe: (
permits: number
) => number
releaseUnsafe(permits: numberpermits)
})
)
})
}
}
}