Skip to content

Examples

When making API calls to third-party services, we may want to enforce timeouts and retry mechanisms. In this example, the API call is set to retry a maximum of two times in case of failure, and the entire operation will be interrupted if it takes longer than 4 seconds.

1
import {
import Console
Console
,
import Effect
Effect
} from "effect"
2
3
// Function to make the API call
4
const
const getJson: (url: string) => Effect.Effect<unknown, UnknownException, never>
getJson
= (
(parameter) url: string
url
: string) =>
5
import Effect
Effect
.
const tryPromise: <unknown>(evaluate: (signal: AbortSignal) => PromiseLike<unknown>) => Effect.Effect<unknown, UnknownException, never> (+1 overload)

Creates an `Effect` that represents an asynchronous computation that might fail. If the `Promise` returned by `evaluate` rejects, the error is caught and the effect fails with an `UnknownException`. An optional `AbortSignal` can be provided to allow for interruption of the wrapped `Promise` API. **Overload with custom error handling:** Creates an `Effect` that represents an asynchronous computation that might fail, with custom error mapping. If the `Promise` rejects, the `catch` function maps the error to an error of type `E`.

tryPromise
(() =>
6
function fetch(input: string | URL | globalThis.Request, init?: RequestInit): Promise<Response>
fetch
(
(parameter) url: string
url
).
(method) Promise<Response>.then<unknown, never>(onfulfilled?: ((value: Response) => unknown) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<unknown>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
((
(parameter) res: Response
res
) => {
7
if (!
(parameter) res: Response
res
.
(property) Response.ok: boolean
ok
) {
8
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) globalThis.Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
("error")
9
throw new
var Error: ErrorConstructor new (message?: string) => Error
Error
(
(parameter) res: Response
res
.
(property) Response.statusText: string
statusText
)
10
}
11
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) globalThis.Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
("ok")
12
return
(parameter) res: Response
res
.
(property) BodyMixin.json: () => Promise<unknown>
json
() as unknown
13
})
14
)
15
16
// Program that retries the API call twice,
17
// times out after 4 seconds, and logs errors
18
const
const program: (url: string) => Effect.Effect<unknown, never, never>
program
= (
(parameter) url: string
url
: string) =>
19
const getJson: (url: string) => Effect.Effect<unknown, UnknownException, never>
getJson
(
(parameter) url: string
url
).
(method) Pipeable.pipe<Effect.Effect<unknown, UnknownException, never>, Effect.Effect<unknown, UnknownException, never>, Effect.Effect<unknown, UnknownException | TimeoutException, never>, Effect.Effect<...>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<...>) => Effect.Effect<...>, bc: (_: Effect.Effect<...>) => Effect.Effect<...>, cd: (_: Effect.Effect<...>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
20
import Effect
Effect
.
const retry: <UnknownException, { times: number; }>(options: { times: number; }) => <A, R>(self: Effect.Effect<A, UnknownException, R>) => Effect.Effect<...> (+3 overloads)

Retries according to the options provided

retry
({
(property) times: number
times
: 2 }),
21
import Effect
Effect
.
const timeout: (duration: DurationInput) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E | TimeoutException, R> (+1 overload)

Returns an effect that will timeout this effect, failing with a `Cause.TimeoutException` if the timeout elapses before the effect has produced a value. If the timeout elapses without producing a value, the running effect will be safely interrupted. WARNING: The effect returned by this method will not itself return until the underlying effect is actually interrupted. This leads to more predictable resource utilization. If early return is desired, then instead of using `effect.timeout(d)`, use `effect.disconnect.timeout(d)`, which first disconnects the effect's interruption signal before performing the timeout, resulting in earliest possible return, before an underlying effect has been successfully interrupted.

timeout
("4 seconds"),
22
import Effect
Effect
.
const catchAll: <any, void, never, never>(f: (e: any) => Effect.Effect<void, never, never>) => <A, R>(self: Effect.Effect<A, any, R>) => Effect.Effect<void | A, never, R> (+1 overload)

Recovers from all recoverable errors. **Note**: that `Effect.catchAll` will not recover from unrecoverable defects. To recover from both recoverable and unrecoverable errors use `Effect.catchAllCause`.

catchAll
(
import Console
Console
.
const error: (...args: ReadonlyArray<any>) => Effect.Effect<void>
error
)
23
)
24
25
// Test the successful case
26
import Effect
Effect
.
const runFork: <unknown, never>(effect: Effect.Effect<unknown, never, never>, options?: RunForkOptions) => RuntimeFiber<unknown, never>

Executes an effect and returns a `RuntimeFiber` that represents the running computation. Use `runFork` when you want to start an effect without blocking the current execution flow. It returns a fiber that you can observe, interrupt, or join as needed.

runFork
(
const program: (url: string) => Effect.Effect<unknown, never, never>
program
("https://dummyjson.com/products/1?delay=1000"))
27
/*
28
Output:
29
ok
30
*/
31
32
// Test case: timeout scenario
33
import Effect
Effect
.
const runFork: <unknown, never>(effect: Effect.Effect<unknown, never, never>, options?: RunForkOptions) => RuntimeFiber<unknown, never>

Executes an effect and returns a `RuntimeFiber` that represents the running computation. Use `runFork` when you want to start an effect without blocking the current execution flow. It returns a fiber that you can observe, interrupt, or join as needed.

runFork
(
const program: (url: string) => Effect.Effect<unknown, never, never>
program
("https://dummyjson.com/products/1?delay=5000"))
34
/*
35
Output:
36
TimeoutException: Operation timed out before the specified duration of '4s' elapsed
37
*/
38
39
// Test case: API error handling
40
import Effect
Effect
.
const runFork: <unknown, never>(effect: Effect.Effect<unknown, never, never>, options?: RunForkOptions) => RuntimeFiber<unknown, never>

Executes an effect and returns a `RuntimeFiber` that represents the running computation. Use `runFork` when you want to start an effect without blocking the current execution flow. It returns a fiber that you can observe, interrupt, or join as needed.

runFork
(
const program: (url: string) => Effect.Effect<unknown, never, never>
program
("https://dummyjson.com/auth/products/1?delay=500"))
41
/*
42
Output:
43
error
44
error
45
error
46
UnknownException: An unknown error occurred
47
*/

Sometimes, we need to retry a failed API call only for specific error conditions, such as certain HTTP status codes.

1
import {
import Console
Console
,
import Effect
Effect
} from "effect"
2
3
// Custom error class for handling status codes
4
class
class Err
Err
extends
var Error: ErrorConstructor
Error
{
5
constructor(
(parameter) message: string
message
: string, readonly
(property) Err.status: number
status
: number) {
6
super(
(parameter) message: string
message
)
7
}
8
}
9
10
// Function to make the API call
11
const
const getJson: (url: string) => Effect.Effect<unknown, Err, never>
getJson
= (
(parameter) url: string
url
: string) =>
12
import Effect
Effect
.
const tryPromise: <unknown, Err>(options: { readonly try: (signal: AbortSignal) => PromiseLike<unknown>; readonly catch: (error: unknown) => Err; }) => Effect.Effect<unknown, Err, never> (+1 overload)

Creates an `Effect` that represents an asynchronous computation that might fail. If the `Promise` returned by `evaluate` rejects, the error is caught and the effect fails with an `UnknownException`. An optional `AbortSignal` can be provided to allow for interruption of the wrapped `Promise` API. **Overload with custom error handling:** Creates an `Effect` that represents an asynchronous computation that might fail, with custom error mapping. If the `Promise` rejects, the `catch` function maps the error to an error of type `E`.

tryPromise
({
13
(property) try: (signal: AbortSignal) => PromiseLike<unknown>
try
: () =>
14
function fetch(input: string | URL | globalThis.Request, init?: RequestInit): Promise<Response>
fetch
(
(parameter) url: string
url
).
(method) Promise<Response>.then<unknown, never>(onfulfilled?: ((value: Response) => unknown) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<unknown>

Attaches callbacks for the resolution and/or rejection of the Promise.

then
((
(parameter) res: Response
res
) => {
15
if (!
(parameter) res: Response
res
.
(property) Response.ok: boolean
ok
) {
16
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) globalThis.Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
(parameter) res: Response
res
.
(property) Response.status: number
status
)
17
throw new
constructor Err(message: string, status: number): Err
Err
(
(parameter) res: Response
res
.
(property) Response.statusText: string
statusText
,
(parameter) res: Response
res
.
(property) Response.status: number
status
)
18
}
19
return
(parameter) res: Response
res
.
(property) BodyMixin.json: () => Promise<unknown>
json
() as unknown
20
}),
21
(property) catch: (error: unknown) => Err
catch
: (
(parameter) e: unknown
e
) =>
(parameter) e: unknown
e
as
class Err
Err
22
})
23
24
// Program that retries only for 401 status codes
25
const
const program: (url: string) => Effect.Effect<unknown, never, never>
program
= (
(parameter) url: string
url
: string) =>
26
const getJson: (url: string) => Effect.Effect<unknown, Err, never>
getJson
(
(parameter) url: string
url
).
(method) Pipeable.pipe<Effect.Effect<unknown, Err, never>, Effect.Effect<unknown, Err, never>, Effect.Effect<unknown, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<unknown, Err, never>) => Effect.Effect<...>, bc: (_: Effect.Effect<...>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
27
import Effect
Effect
.
const retry: <Err, { while: (err: Err) => boolean; }>(options: { while: (err: Err) => boolean; }) => <A, R>(self: Effect.Effect<A, Err, R>) => Effect.Effect<A, Err, R> (+3 overloads)

Retries according to the options provided

retry
({
(property) while: (err: Err) => boolean
while
: (
(parameter) err: Err
err
) =>
(parameter) err: Err
err
.
(property) Err.status: number
status
=== 401 }),
28
import Effect
Effect
.
const catchAll: <any, void, never, never>(f: (e: any) => Effect.Effect<void, never, never>) => <A, R>(self: Effect.Effect<A, any, R>) => Effect.Effect<void | A, never, R> (+1 overload)

Recovers from all recoverable errors. **Note**: that `Effect.catchAll` will not recover from unrecoverable defects. To recover from both recoverable and unrecoverable errors use `Effect.catchAllCause`.

catchAll
(
import Console
Console
.
const error: (...args: ReadonlyArray<any>) => Effect.Effect<void>
error
)
29
)
30
31
// Test the 401 scenario
32
import Effect
Effect
.
const runFork: <unknown, never>(effect: Effect.Effect<unknown, never, never>, options?: RunForkOptions) => RuntimeFiber<unknown, never>

Executes an effect and returns a `RuntimeFiber` that represents the running computation. Use `runFork` when you want to start an effect without blocking the current execution flow. It returns a fiber that you can observe, interrupt, or join as needed.

runFork
(
33
const program: (url: string) => Effect.Effect<unknown, never, never>
program
("https://dummyjson.com/auth/products/1?delay=1000")
34
)
35
/*
36
Output:
37
401
38
401
39
401
40
401
41
...
42
*/
43
44
// Test the 404 scenario
45
import Effect
Effect
.
const runFork: <unknown, never>(effect: Effect.Effect<unknown, never, never>, options?: RunForkOptions) => RuntimeFiber<unknown, never>

Executes an effect and returns a `RuntimeFiber` that represents the running computation. Use `runFork` when you want to start an effect without blocking the current execution flow. It returns a fiber that you can observe, interrupt, or join as needed.

runFork
(
const program: (url: string) => Effect.Effect<unknown, never, never>
program
("https://dummyjson.com/-"))
46
/*
47
Output:
48
404
49
Err [Error]: Not Found
50
*/

Sometimes, we need to run a task periodically until a longer-running task completes. This is common in scenarios like polling or periodic status logging.

1
import {
import Effect
Effect
,
import Console
Console
,
import Schedule
Schedule
} from "effect"
2
3
const
const longRunningEffect: Effect.Effect<void, never, never>
longRunningEffect
=
import Console
Console
.
const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log
("done").
(method) Pipeable.pipe<Effect.Effect<void, never, never>, Effect.Effect<void, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<void, never, never>) => Effect.Effect<void, never, never>): Effect.Effect<...> (+21 overloads)
pipe
(
4
import Effect
Effect
.
const delay: (duration: DurationInput) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R> (+1 overload)

Returns an effect that is delayed from this effect by the specified `Duration`.

delay
("5 seconds")
5
)
6
7
const
const action: Effect.Effect<void, never, never>
action
=
import Console
Console
.
const log: (...args: ReadonlyArray<any>) => Effect.Effect<void>
log
("action...")
8
9
const
const schedule: Schedule.Schedule<number, unknown, never>
schedule
=
import Schedule
Schedule
.
const fixed: (interval: DurationInput) => Schedule.Schedule<number>

A schedule that recurs on a fixed interval. Returns the number of repetitions of the schedule so far. If the action run between updates takes longer than the interval, then the action will be run immediately, but re-runs will not "pile up". ``` |-----interval-----|-----interval-----|-----interval-----| |---------action--------||action|-----|action|-----------| ```

fixed
("1.5 seconds")
10
11
const
const program: Effect.Effect<number | void, never, never>
program
=
import Effect
Effect
.
const race: <number, never, never, void, never, never>(self: Effect.Effect<number, never, never>, that: Effect.Effect<void, never, never>) => Effect.Effect<number | void, never, never> (+1 overload)

Returns an effect that races this effect with the specified effect, returning the first successful `A` from the faster side. If one effect succeeds, the other will be interrupted. If neither succeeds, then the effect will fail with some error.

race
(
12
import Effect
Effect
.
const repeat: <void, never, never, number, never>(self: Effect.Effect<void, never, never>, schedule: Schedule.Schedule<number, void, never>) => Effect.Effect<number, never, never> (+3 overloads)

The `repeat` function returns a new effect that repeats the given effect according to a specified schedule or until the first failure. The scheduled recurrences are in addition to the initial execution, so `Effect.repeat(action, Schedule.once)` executes `action` once initially, and if it succeeds, repeats it an additional time.

repeat
(
const action: Effect.Effect<void, never, never>
action
,
const schedule: Schedule.Schedule<number, unknown, never>
schedule
),
13
const longRunningEffect: Effect.Effect<void, never, never>
longRunningEffect
14
)
15
16
import Effect
Effect
.
const runPromise: <number | void, never>(effect: Effect.Effect<number | void, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<number | void>

Executes an effect and returns a `Promise` that resolves with the result. Use `runPromise` when working with asynchronous effects and you need to integrate with code that uses Promises. If the effect fails, the returned Promise will be rejected with the error.

runPromise
(
const program: Effect.Effect<number | void, never, never>
program
)
17
/*
18
Output:
19
action...
20
action...
21
action...
22
action...
23
done
24
*/